module Numeric.Noise.Random (
randomInt,
randomInts,
shuffle
) where
import Numeric.Noise
import Data.Bits
randomInt :: Seed -> (Int, Seed)
randomInt seed = randomInt' seed 0 16
randomInt' :: Int -> Seed -> Int -> (Int, Seed)
randomInt' r seed 0 = (r, seed)
randomInt' r seed n = randomInt' r' seed' (n 1)
where seed' = 3039177861 * seed + 1
r' = shiftL r 2 + shiftR seed' 30
randomInts :: Seed -> Int -> [Int]
randomInts _ 0 = []
randomInts seed n = r : randomInts seed' (n 1)
where (r, seed') = randomInt seed
shuffle :: [a] -> Seed -> [a]
shuffle xs seed = fst (shuffle' xs [] seed)
shuffle' :: [a] -> [a] -> Seed -> ([a], Seed)
shuffle' [] acc seed = (acc, seed)
shuffle' xs acc seed = shuffle' (h ++ ys) (y:acc) seed'
where (seed', r) = randomInt seed
(h, y:ys) = splitAt (r `mod` (length xs)) xs