The reality of Stefan Scholl

"I hope there's pudding"

Randomizing a sequence in Factor

In Trivial Sequence Shuffling, Leslie P. Polzer posted an easy and short version of a sequence shuffler in Common Lisp. The sort function has to do all the work. Not a recommended way to shuffle your sequences, but a short one.

The Factor version isn't very long, too:

USING: sorting random ;
: seqrnd ( seq -- rndseq ) [ 2drop { -1 0 1 } random ] sort ;

! Testing it:
"Trivial Sequence Shuffling in Factor" seqrnd .
"rloFt necanagi ilcfefquh SneuiSrivT"

But you should rather use a Fisher-Yates shuffle.

UPDATE (2009-11-08):
Doesn't work anymore in current versions of Factor. Needs a few changes:

USING: sorting math.order random ;
: seqrnd ( seq -- rndseq ) [ 2drop { +lt+ +eq+ +gt+ } random ] sort ;

! Testing it:
"Trivial Sequence Shuffling in Factor" seqrnd >string .
"cfie S onirateS vT lrcqFuengihufilna"
Tags: Factor