Problem 24

99 Haskell Problems

Problem 24

Lotto: Draw N different random numbers from the set 1..M.

Example:

* (rnd-select 6 49)
(23 1 17 33 21 37)

Example in Haskell:

Prelude System.Random>diff_select 6 49
Prelude System.Random>[23,1,17,33,21,37]

Solution:

Using the nub solution from Problem 23 to remove any duplicates, this become very easy.

diff_select n m = rnd_select [1..m] n

With rnd_select as:

rnd_select :: [a] -> Int -> [a]
rnd_select x n = map (x!!) is
 where is = take n . nub $ randomRs (0, length x - 1) (mkStdGen 100)

Additional References:

Advertisements