You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
655 B
32 lines
655 B
module RandomExample where |
|
|
|
import System.Random |
|
|
|
-- Six-sided die |
|
data Die = |
|
DieOne |
|
| DieTwo |
|
| DieThree |
|
| DieFour |
|
| DieFive |
|
| DieSix |
|
deriving (Eq, Show) |
|
|
|
intToDie :: Int -> Die |
|
intToDie n = case n of |
|
1 -> DieOne |
|
2 -> DieTwo |
|
3 -> DieThree |
|
4 -> DieFour |
|
5 -> DieFive |
|
6 -> DieSix |
|
-- Use 'error' _extremely_ sparingly |
|
x -> error $ "intToDie got non 1-6 integer: " ++ show x |
|
|
|
rollDieThreeTimes :: (Die, Die, Die) |
|
rollDieThreeTimes = do |
|
let s = mkStdGen 0 |
|
(d1, s1) = randomR (1, 6) s |
|
(d2, s2) = randomR (1, 6) s1 |
|
(d3, _) = randomR (1, 6) s2 |
|
(intToDie d1, intToDie d2, intToDie d3) |