fix random for real. is there redundant math here?

db4
Doug Coleman 2008-09-09 17:07:32 -05:00
parent d72114fcce
commit 24642501e7
2 changed files with 7 additions and 5 deletions

View File

@ -1,4 +1,5 @@
USING: random sequences tools.test kernel math math.functions ; USING: random sequences tools.test kernel math math.functions
sets ;
IN: random.tests IN: random.tests
[ 4 ] [ 4 random-bytes length ] unit-test [ 4 ] [ 4 random-bytes length ] unit-test
@ -12,3 +13,5 @@ IN: random.tests
[ t ] [ 10000 [ 0 [ drop 187 random + ] reduce ] keep / 2 * 187 10 ~ ] unit-test [ t ] [ 10000 [ 0 [ drop 187 random + ] reduce ] keep / 2 * 187 10 ~ ] unit-test
[ t ] [ 10000 [ 0 [ drop 400 random + ] reduce ] keep / 2 * 400 10 ~ ] unit-test [ t ] [ 10000 [ 0 [ drop 400 random + ] reduce ] keep / 2 * 400 10 ~ ] unit-test
[ t ] [ 1000 [ 400 random ] replicate prune length 256 > ] unit-test

View File

@ -36,10 +36,9 @@ M: f random-32* ( obj -- * ) no-random-number-generator ;
: random ( seq -- elt ) : random ( seq -- elt )
[ f ] [ [ f ] [
[ [
length [ length dup log2 7 + 8 /i 1+ random-bytes
log2 8 + 8 /i [ length 3 shift 2^ ] [ byte-array>bignum ] bi
random-bytes byte-array>bignum swap / * >integer
] keep mod
] keep nth ] keep nth
] if-empty ; ] if-empty ;