# Difference between revisions of "99 questions/Solutions/24"

From HaskellWiki

< 99 questions | Solutions

Line 17: | Line 17: | ||

In order to use randomRIO here, we need import module System.Random. |
In order to use randomRIO here, we need import module System.Random. |
||

+ | |||

+ | As can be seen, having implemented problem 23, rnd_select, the solution is trivial. |
||

+ | <haskell> |
||

+ | diff_select n to = rnd_select [1..to] n |
||

+ | </haskell> |

## Revision as of 05:56, 20 November 2010

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

```
import System.Random
diff_select :: Int -> Int -> IO [Int]
diff_select n to = diff_select' n [1..to]
diff_select' 0 _ = return []
diff_select' _ [] = error "too few elements to choose from"
diff_select' n xs = do r <- randomRIO (0,(length xs)-1)
let remaining = take r xs ++ drop (r+1) xs
rest <- diff_select' (n-1) remaining
return ((xs!!r) : rest)
```

The random numbers have to be distinct!

In order to use randomRIO here, we need import module System.Random.

As can be seen, having implemented problem 23, rnd_select, the solution is trivial.

```
diff_select n to = rnd_select [1..to] n
```