Random Numbers for the beginner ?

Hi I have tried I swear, even googled for 45 minutes, but I cant seem to get random numbers working. In the documentation is has: rollDice :: IO Int rollDice = getStdRandom (randomR (1,6)) But if I type "getStdRandom (randomR (1,6))" into hugs in the context of module which imports Random, I get get errors. ERROR - Unresolved overloading *** Type : (Random a, Num a) => IO a *** Expression : getStdRandom (randomR (1,6)) Roll dice takes no parameters and returns an IO Int. So in thoery (mine at least ;-) ) running this as an expresion should work. I should get an IO Int back from the interpreter ? 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 ? 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. Thanks, C _________________________________________________________________ Add photos to your e-mail with MSN 8. Get 2 months FREE*. http://join.msn.com/?page=features/featuredemail

Crypt Master, CM> I have tried I swear, even googled for 45 minutes, but I CM> cant seem to get random numbers working. CM> CM> In the documentation is has: CM> CM> rollDice :: IO Int CM> rollDice = getStdRandom (randomR (1,6)) CM> CM> But if I type "getStdRandom (randomR (1,6))" into hugs CM> in the context of module which imports Random, I get get CM> errors. CM> CM> ERROR - Unresolved overloading CM> *** Type : (Random a, Num a) => IO a CM> *** Expression : getStdRandom (randomR (1,6)) Use (getStdRandom (randomR (1,6))) :: IO Int instead. \begin{code} module Main where import Random main :: IO () main = do n <- (getStdRandom (randomR (1,6))) :: IO Int ; print n \end{code} Explanation. The compiler/interpreter knows your expressing is producing a value of type IO a. It also knows a is an instance of Num. But it just does not have enough information to resolve the exact type a. So, you have to help the compiler/interpreter a little and provide the information it needs through explicit type signatures or annotations. HTH, Stefan

On Tue, 06 Jul 2004 19:09:53 +0000
"Crypt Master"
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
participants (3)
-
Crypt Master
-
Koji Nakahara
-
Stefan Holdermans