shuffle.hs

Haskell

Public Domain

Randomly reorder the elements of a list

Download (right click, save as, rename as appropriate)

Embed

1
2
3
4
5
6
import Random

shuffle :: RandomGen g => [a] -> g -> ([a], g)
shuffle xs rng = foldl (\(ln, g) x ->
 let (n, g') = randomR (0, length ln) g
 in (take n ln ++ [x] ++ drop n ln, g')) ([], rng) xs