19 lines
524 B
Haskell
19 lines
524 B
Haskell
|
module Random where
|
||
|
|
||
|
import System.Random
|
||
|
import 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] k)
|