24 lines
563 B
Haskell
24 lines
563 B
Haskell
module Shuffle (
|
|
shuffle
|
|
) where
|
|
|
|
import Data.List
|
|
import System.Random
|
|
|
|
seed = 20
|
|
|
|
-- Shuffle a list of values.
|
|
shuffle :: [a] -> [a]
|
|
shuffle l = map (l !!) $ generateIndices $ length l
|
|
|
|
-- Generate indices to map the elements of a list over so that they
|
|
-- are randomly shuffled.
|
|
generateIndices :: Int -> [Int]
|
|
generateIndices size = take size uniqueList
|
|
where randomList = randomRs (0, size - 1) randomGen
|
|
uniqueList = nub randomList
|
|
|
|
-- Generate a random generator
|
|
-- TODO Écht random maken?
|
|
randomGen :: StdGen
|
|
randomGen = mkStdGen seed
|