unit test how many elements to swap with randomize, remove a usage of pick

db4
Doug Coleman 2009-02-14 13:28:41 -06:00
parent 0286c524e8
commit 1418a67cc6
2 changed files with 9 additions and 2 deletions

View File

@ -1,5 +1,5 @@
USING: random sequences tools.test kernel math math.functions
sets grouping ;
sets grouping random.private ;
IN: random.tests
[ 4 ] [ 4 random-bytes length ] unit-test
@ -20,3 +20,6 @@ IN: random.tests
[ f ]
[ 100 [ { 0 1 } random ] replicate all-equal? ] unit-test
[ t ]
[ { 1 2 } [ length ] [ >randomize-range length ] bi - 1 = ] unit-test

View File

@ -43,6 +43,9 @@ M: f random-32* ( obj -- * ) no-random-number-generator ;
[ random-bytes >byte-array byte-array>bignum ]
[ 3 shift 2^ ] bi / * >integer ;
: >randomize-range ( seq -- range )
length 1+ 2 (a,b] ; inline
PRIVATE>
: random-bits ( n -- r ) 2^ random-integer ;
@ -53,7 +56,8 @@ PRIVATE>
] if-empty ;
: randomize ( seq -- seq' )
dup length 1+ 2 (a,b] [ [ random ] [ 1- ] bi pick exchange ] each ;
[ ] [ >randomize-range ] [ ] tri
'[ [ random ] [ 1- ] bi _ exchange ] each ;
: delete-random ( seq -- elt )
[ length random-integer ] keep [ nth ] 2keep delete-nth ;