added:
authorClaus Gittinger <cg@exept.de>
Wed, 30 Sep 2009 12:30:10 +0200
changeset 12051 39b46202893c
parent 12050 bd0013f79e4f
child 12052 7aabd3b316a6
added: #shuffled #shuffledBy:
SequenceableCollection.st
--- a/SequenceableCollection.st	Tue Sep 29 22:31:39 2009 +0200
+++ b/SequenceableCollection.st	Wed Sep 30 12:30:10 2009 +0200
@@ -358,6 +358,7 @@
     ^ self == SequenceableCollection
 ! !
 
+
 !SequenceableCollection methodsFor:'Compatibility-Squeak'!
 
 allButFirst
@@ -596,6 +597,34 @@
     ^ self copyFrom:2
 !
 
+shuffled
+    "return a randomly shuffled copy of the receiver"
+
+    ^ self shuffledBy:Random
+
+    "
+     #(1 2 3 4 5 6 7 8 9) shuffled.
+     #(1 2 3 4 5 6 7 8 9) shuffled. 
+    "
+!
+
+shuffledBy:aRandom
+    "return a randomly shuffled copy of the receiver, using the given random generator"
+
+    |copy|
+
+    copy := self shallowCopy.
+    copy size to: 1 by: -1 do:[:i | 
+        copy swap: i with: ((1 to: i) atRandom: aRandom)
+    ].
+    ^ copy
+
+    "
+     #(1 2 3 4 5 6 7 8 9) shuffledBy:(Random new).
+     #(1 2 3 4 5 6 7 8 9) shuffledBy:(Random new). 
+    "
+!
+
 sortBy:aBlock
     "Sort inplace - destructive"
 
@@ -610,6 +639,7 @@
     ^ self replaceFrom:start to:stop with:anArray startingAt:repStart
 ! !
 
+
 !SequenceableCollection methodsFor:'accessing'!
 
 after:anObject
@@ -5773,6 +5803,7 @@
     "Created: 14.2.1997 / 16:13:03 / cg"
 ! !
 
+
 !SequenceableCollection methodsFor:'searching'!
 
 detect:aBlock startingAt:startIndex
@@ -7653,7 +7684,11 @@
 !SequenceableCollection class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/SequenceableCollection.st,v 1.282 2009-09-28 13:25:19 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/SequenceableCollection.st,v 1.283 2009-09-30 10:30:10 cg Exp $'
+!
+
+version_CVS
+    ^ '$Header: /cvs/stx/stx/libbasic/SequenceableCollection.st,v 1.283 2009-09-30 10:30:10 cg Exp $'
 ! !
 
 SequenceableCollection initialize!