From 1418a67cc63e94a72efd09d30a9acd6eb163467c Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Sat, 14 Feb 2009 13:28:41 -0600 Subject: [PATCH] unit test how many elements to swap with randomize, remove a usage of pick --- basis/random/random-tests.factor | 5 ++++- basis/random/random.factor | 6 +++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/basis/random/random-tests.factor b/basis/random/random-tests.factor index 63c2159ab6..160a4fced1 100644 --- a/basis/random/random-tests.factor +++ b/basis/random/random-tests.factor @@ -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 diff --git a/basis/random/random.factor b/basis/random/random.factor index cb4a141a98..5befee7339 100755 --- a/basis/random/random.factor +++ b/basis/random/random.factor @@ -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 ;