alea/Alea/Random.hs

19 lines
534 B
Haskell
Raw Normal View History

2014-10-14 18:09:58 +02:00
module Alea.Random where
2014-10-13 21:26:55 +02:00
import System.Random
2014-10-14 18:09:58 +02:00
import Alea.List
2014-10-13 21:26:55 +02:00
-- 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] k)