34 lines
1,017 B
Haskell
34 lines
1,017 B
Haskell
module Shuffle
|
|
( shuffle
|
|
) where
|
|
|
|
import Data.List
|
|
import System.Random
|
|
|
|
----------------------------------------------------------------------
|
|
-- Shuffle a list so that the elements of the list are randomly --
|
|
-- perumated. --
|
|
----------------------------------------------------------------------
|
|
|
|
----------------------------- Constants ------------------------------
|
|
|
|
-- The seed used to generate random numbers.
|
|
seed :: Int
|
|
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
|
|
randomGen :: StdGen
|
|
randomGen = mkStdGen seed
|