1
Fork 0
This repository has been archived on 2023-12-08. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
2022FuncProg-project2-patience/lib/Shuffle.hs

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