Random number generator

Tomasz Zielonka t.zielonka at students.mimuw.edu.pl
Fri Jan 16 07:24:13 EST 2004

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,

.signature: Too many levels of symbolic links

More information about the Glasgow-haskell-users mailing list