From 0286c524e84bc58e2ca430933daf5c68ffd7b6a5 Mon Sep 17 00:00:00 2001
From: Doug Coleman <doug.coleman@gmail.com>
Date: Sat, 14 Feb 2009 13:14:00 -0600
Subject: [PATCH 1/2] randomize was broken -- elements could not exchange with
 themselves

---
 basis/random/random-tests.factor | 5 ++++-
 basis/random/random.factor       | 2 +-
 2 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/basis/random/random-tests.factor b/basis/random/random-tests.factor
index e686dd7301..63c2159ab6 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 ;
+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
diff --git a/basis/random/random.factor b/basis/random/random.factor
index 554ed5c96a..cb4a141a98 100755
--- a/basis/random/random.factor
+++ b/basis/random/random.factor
@@ -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 ;

From 1418a67cc63e94a72efd09d30a9acd6eb163467c Mon Sep 17 00:00:00 2001
From: Doug Coleman <doug.coleman@gmail.com>
Date: Sat, 14 Feb 2009 13:28:41 -0600
Subject: [PATCH 2/2] 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 ;