# [gforth] Random number generator

```Hi James,

```
My false assumption was that the return from rnd
is always equal to (rnd) (i.e. assuming a shift
register based LCG). In that case I deduced that
when n is stored in (rnd), and after this a sequence
n1,...nk n is returned by rnd, the generator length
is k.

How embarrasing!

At least the futile computations gave me a number
that makes it trivial to find the real generator
length.

-marcel

-- ------------------------
variable (rnd)

: rnd ( -- n )
(rnd) 32B@
dup #13   lshift xor \$FFFFFFFF and
dup #17   rshift xor \$FFFFFFFF and
dup DUP 5 lshift xor (rnd) 32B! ;

-- A good seed to start is 1073741825, because it
-- starts a sequence with the number 1073741825. Therefore
-- we skip this first returned number and test until
-- 1073741825 is returned again.

: xtest ( -- )
CR ." Searching for the generator cycle ... cycle = "
TIMER-RESET
#1073741825 (rnd) ! rnd ( 1073741825) DROP
0 BEGIN  rnd #1073741825 <>  WHILE  1+  REPEAT
U.
CR .ELAPSED ;

\ FORTH> xtest
\ Searching for the generator cycle ... cycle = 4294967294
\ 47.466 seconds elapsed. ok
\ FORTH> 4294967294 H. \$FFFFFFFE ok

```