[Haskell-cafe] Linear shuffle

Henning Thielemann iakd0 at clusterf.urz.uni-halle.de
Fri Jan 14 09:18:01 EST 2005


On Fri, 14 Jan 2005, Scott Turner wrote:

> The shuffling algorithms mentioned so far are comparable to 
> insertion/selection sort. I had come up with a shuffler that relates to 
> quicksort, in that it partitions the input randomly into lists and works 
> recursively from there. It looks efficient and works out well in Haskell.


I did some shuffling based on mergesort, that is a list is randomly split
(unzipped) into two lists and the parts are concatenated afterwards. You
must repeat this some times. It even works for infinite lists. It doesn't
need IO.

> randomPermute :: RandomGen g => [a] -> g -> ([a],g)
> randomPermute x g0 =
>    let (choices,g1) = runState (mapM (const (State (randomR
(False,True)))) x) g0
>        xc = zip x choices
>    in  (map fst (filter snd xc ++ filter (not . snd) xc), g1)



More information about the Haskell-Cafe mailing list