Random number generator

Hi, I need some assistances in calling random number generator using 6.0.1 haskell compiler. To return a list of random int [Int], I have tried the following: drawInt :: Int->Int -> [Int] drawInt x y = getStdRandom (randomRs (x,y)) It has a type error. I would appreciate if you know how to fix it. Thank you in advance. Tina Yu http://www.improvise.ws --------------------------------- Do you Yahoo!? Yahoo! Hotjobs: Enter the "Signing Bonus" Sweepstakes

That's definitely not a message for the bugs list :-) Please have a look at this page: http://www.zvon.org/other/haskell/Outputrandom/getStdRandom_f.html It gives the correct signature for drawInt as you defined it: Int -> Int -> IO Int The signature you gave doesn't work because it specifies a pure function - for the same set of arguments, it must always produce the same result. How many ints do you want to generate? I don't think it is possible to generate an infinite lazy list in this case because this interferes with monad semantics. If you want a fixed number of random ints, try this: drawInts :: Int -> Int -> Int -> IO [Int] drawInts num x y = sequence (replicate num (getStdRandom (randomR (x,y)))) -Stefan Gwoing Yu wrote:
Hi,
I need some assistances in calling random number generator using 6.0.1 haskell compiler. To return a list of random int [Int], I have tried the following:
drawInt :: Int->Int -> [Int] drawInt x y = getStdRandom (randomRs (x,y))
It has a type error. I would appreciate if you know how to fix it.
Thank you in advance.

Thanks Stefan,
Sorry for sending the message to the bug list. My first post to
the users mailing list seems to get lost somewhere. I therefore try
my luck on the bug list. It seems to work :)
I am extending my polyGP system using the new haskell compiler.
The random number is to be used for selecting crossover/mutation
points, not to be printed (not IO type). I tried the following and they
seem to work fine.
-- get a list of random int between 1 and 250
drawInt :: [Int]
drawInt = randomRs (1, 250)(mkStdGen 10)
-- get a list of random double between 0 and 100
drawDouble :: [Double]
drawDouble = randomRs (0, 100) (mkStdGen 100)
Are they really random numbers ? I am wodering why all
random double has values < 1.0. Do you know ? Thanks !!
Tina
Stefan Reich
Hi,
I need some assistances in calling random number generator using 6.0.1 haskell compiler. To return a list of random int [Int], I have tried the following:
drawInt :: Int->Int -> [Int] drawInt x y = getStdRandom (randomRs (x,y))
It has a type error. I would appreciate if you know how to fix it.
Thank you in advance.
Tina Yu http://www.improvise.ws
--------------------------------- Do you Yahoo!? Yahoo! Hotjobs: Enter the "Signing Bonus" Sweepstakes

On Thu, Jan 15, 2004 at 11:00:24PM +0100, Stefan Reich wrote:
How many ints do you want to generate? I don't think it is possible to generate an infinite lazy list in this case because this interferes with monad semantics. If you want a fixed number of random ints, try this:
drawInts :: Int -> Int -> Int -> IO [Int] drawInts num x y = sequence (replicate num (getStdRandom (randomR (x,y))))
Sure you can. You can 'split' the random generator getting two new generators. One is used to update the global generator, and you use the second one to generate infinite list of pseudo-random values. drawInts :: Int -> Int -> IO [Int] drawInts x y = getStdRandom split >>= (return . randomR (x, y)) Actually, if you relax the type signature you get a function that's "missing" from Random module: randomRIOs :: (Random a) => (a, a) -> IO [a] randomRIOs r = getStdRandom split >>= (return . randomRs r) Best regards, Tom -- .signature: Too many levels of symbolic links
participants (3)
-
Gwoing Yu
-
Stefan Reich
-
Tomasz Zielonka