[Haskell-cafe] System.Random.Shuffle fix

Manlio Perillo manlio_perillo at libero.it
Mon Mar 23 13:36:18 EDT 2009

friggin friggin ha scritto:
> I was looking for a shuffling algorithm to shuffle mp3-playlists so was 
> very happy to see System.Random.Shuffle:
> http://hackage.haskell.org/cgi-bin/hackage-scripts/package/random-shuffle-0.0.2
> However I get  errors,non-exhaustive patterns in function shufleTree or 
> extractTree depending how I call it. Errors are at the bottom.

During building you should only get warnings.
Non exhaustive patterns are ok, you hit them only if the input data is 

Probably in these cases, error should be used.

> I fixed it but I don't have the math skills to see if I perhaps broke it 
> statistically ...
> Here is my fix, someone (don't remember who, helped me a little):
> http://hpaste.org/fastcgi/hpaste.fcgi/view?id=2789#a2789
> the shuffle at the end is with the fix.
> *Freet> S.shuffle [1..10] [1..3]

Your input is not correct.
If you read the source code (in a future version I'll add Haddock support):

-- Given a sequence (e1,...en) to shuffle, and a sequence
-- (r1,...r[n-1]) of numbers such that r[i] is an independent sample
-- from a uniform random distribution [0..n-i], compute the
-- corresponding permutation of the input sequence.

I have added a convenience function `shuffle'`, where you just need to 
supply a random number generator.

Note that the shuffle' function contains a "bug";
it should return the new random generator:
     shuffle' :: RandomGen gen => [a] -> Int -> gen -> ([a], gen)

I'm going to fix it in next version.

 > [...]

Regards  Manlio

More information about the Haskell-Cafe mailing list