Home Forums Tips & Techniques Smalltalk example to shuffle an OrderedCollection with a Seed

Tagged: 

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #2121
    Cristian Vogel
    Participant

    I found out the built in Kyma ‘anOrderedCollection shuffle’  was not seedable, so I came up with this block closure that does it.

     

    | panPositions shuffler |

    panPositions := ?panPositions asOrderedCollection.

    shuffler := [:coll |
    |n k r|
    n := coll size.
    r := Random new seededWith: ?seed.
    [ n > 1 ] whileTrue: [
    k := r nextIntegerBetween: 1 and: n.
    coll swap: n with: k.
    n := n – 1.
    ]
    ].

    ?shufflePanPositions varIfTrue: [

    shuffler value: panPositions .
    ]

    ifFalse: [ nil ].

    “self debug: panPositions.”

    (inputs at: 1)
    start: 0 s
    panPositions: panPositions

Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.