randomize was broken -- elements could not exchange with themselves

db4
Doug Coleman 2009-02-14 13:14:00 -06:00
parent b638a35fd7
commit 0286c524e8
2 changed files with 5 additions and 2 deletions

View File

@ -1,5 +1,5 @@
USING: random sequences tools.test kernel math math.functions
sets ;
sets grouping ;
IN: random.tests
[ 4 ] [ 4 random-bytes length ] unit-test
@ -17,3 +17,6 @@ IN: random.tests
[ t ] [ 1000 [ 400 random ] replicate prune length 256 > ] unit-test
[ f ] [ 0 random ] unit-test
[ f ]
[ 100 [ { 0 1 } random ] replicate all-equal? ] unit-test

View File

@ -53,7 +53,7 @@ PRIVATE>
] if-empty ;
: randomize ( seq -- seq' )
dup length 1 (a,b] [ dup random pick exchange ] each ;
dup length 1+ 2 (a,b] [ [ random ] [ 1- ] bi pick exchange ] each ;
: delete-random ( seq -- elt )
[ length random-integer ] keep [ nth ] 2keep delete-nth ;