fix random scoping issue

cvs
Slava Pestov 2005-10-10 04:08:51 +00:00
parent e0fdc89126
commit 01740ac7fb
2 changed files with 19 additions and 17 deletions

View File

@ -49,16 +49,20 @@ IN: math
: init-random ( seed -- )
#! Initialize the random number generator with a new seed.
mt-n zero-array swap
HEX: ffffffff bitand 0 pick set-nth
mt-n 1- [ 2dup mt-formula 1+ pick pick 1+ swap set-nth ] repeat
mt set 0 mti set
generate-mt ;
global [
mt-n zero-array swap
HEX: ffffffff bitand 0 pick set-nth
mt-n 1- [ 2dup mt-formula 1+ pick pick 1+ swap set-nth ] repeat
mt set 0 mti set
generate-mt
] bind ;
: (random-int) ( -- rand )
#! Generate a random integer between 0 and 2^32-1 inclusive.
mti get dup mt-n < [ drop generate-mt 0 ] unless
mt-nth mt-temper mti inc ;
global [
mti get dup mt-n < [ drop generate-mt 0 ] unless
mt-nth mt-temper mti inc
] bind ;
: random-int ( n -- rand )
#! Generate a random integer between 0 and n-1 inclusive.

View File

@ -1,14 +1,12 @@
IN: temporary
USE: kernel
USE: lists
USE: math
USE: namespaces
USE: random
USE: test
USE: errors
USING: errors kernel math namespaces sequences test ;
: check-random-int ( max -- )
dup random-int 0 rot between?
[ "Assertion failed" throw ] unless ;
>r random-int 0 r> between? ;
[ ] [ 100 [ 674 check-random-int ] times ] unit-test
[ t ] [ 100 [ 674 check-random-int ] all? ] unit-test
: make-100-random-ints
[ 100 [ 100 random-int , ] times ] { } make ;
[ f ] [ make-100-random-ints make-100-random-ints = ] unit-test