[Haskell-cafe] Random Numbers for the beginner ?

Koji Nakahara yu- at div.club.ne.jp
Tue Jul 6 17:48:12 EDT 2004


On Tue, 06 Jul 2004 19:09:53 +0000
"Crypt Master" <cryptmaster at hotmail.com> wrote:

> So I added RollDice to my module. This doesnt error, but it doesnt return 
> anything except blank spaces:
> 
> HasGal> rollDice
> 
> HasGal>
> 
> Integers or nums should automatically have show correct? So this should show 
> me something ?

rollDice is an action of type IO Int, not a number of type Int.

Try 
Main> do n <- rollDice; print n

> Ultimatly I want to get randomRs infinite list working so I can build
> 
>             randNums = (take (length popList) [1..])
> 
> where the length of pop list is how many random numbers I want.  My code 
> works as it, just need to replace [1..] with some random numbers.

Example:
----
import Random

rollDices :: RandomGen g => g -> [Int]
rollDices = randomRs (1, 6)

randNumInfSeq :: [Int]
randNumInfSeq = rollDices (mkStdGen 0)
---

Main> do g <- getStdGen; let randNums = take 20 $ rollDices g in print randNums
[5,3,3,5,1,6,5,6,2,2,6,2,3,6,2,5,2,6,1,4]

Main> print $ take 20 $ randNumInfSeq
[6,6,4,1,5,2,4,2,2,1,6,5,1,5,3,2,3,4,4,1]


Hope it helps,
Koji Nakahara


More information about the Haskell-Cafe mailing list