The following program in gforth prints out 10 random numbers between 0 and 2:
require random.fs
: main 10 0 do i cr . 3 random . loop ;
main
The problem is, that after each start, the numbers are the same. That means no time(0) seed was used. How can i get random numbers which are different on each start?
In the GPL-licensed source file random.fs
:
Variable seed
$10450405 Constant generator
: rnd ( -- n ) seed @ generator um* drop 1+ dup seed ! ;
: random ( n -- 0..n-1 ) rnd um* nip ;
To grab a random number you can seed the variable seed
with a line, perhaps like this:
utime drop seed !
However I'm no cryptographer, but I'm under the impression seeding with the current time isn't cryptographically secure. I wouldn't use this in any production code.