alea/Alea/Random.hs
Rnhmjoj 8e383dde3f Bugfix
This crashed the program if you were very unlucky.
2014-10-17 00:13:12 +02:00

19 lines
536 B
Haskell

module Alea.Random where
import System.Random
import Alea.List
-- Get n random numbers from the list ys
randPick :: (Eq a, RandomGen g) => [a] -> Int -> g -> ([a], g)
randPick [] _ gen = ([], gen)
randPick _ 0 gen = ([], gen)
randPick ys n gen = (x : xs', gen'')
where
(randIndex, gen') = randomR (0, length ys - 1) gen
(x, xs) = removeAt randIndex ys
(xs', gen'') = randPick xs (n-1) gen'
-- Generate k random number in the range [0, n)
randWords :: Int -> Int -> IO [Int]
randWords n k = getStdRandom (randPick [0..n-1] k)