Random number generator

Gwoing Yu gwoing_yu at yahoo.com
Thu Jan 15 18:13:35 EST 2004

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 !!
Stefan Reich <doc at drjava.de> wrote:
That's definitely not a message for the bugs list :-)

Please have a look at this page: 

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))))


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.
> Tina Yu
> http://www.improvise.ws 

Do you Yahoo!?
Yahoo! Hotjobs: Enter the "Signing Bonus" Sweepstakes-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://haskell.org/pipermail/glasgow-haskell-users/attachments/20040115/1ca3202e/attachment.htm

More information about the Glasgow-haskell-users mailing list