[Haskell] Laziness and the IO Monad (randomness)

David Brown haskell2 at davidb.org
Thu Mar 1 13:17:19 EST 2007


Dave Tapley wrote:

> This code show a trivial case where randomness (and hence the IO
> monad) is not used and the first 10 elements of the produced list
> are printed:

You don't need the IO monad to achieve pseudy-randomness.  Why not use
'randoms' from System.Random (or 'randomRs' for a range).

  take 10 $ (randomRs (1,6) (mkStdGen 1)) :: [Int]

You can use the IO monad to get a randomly seeded generator from the
outside, but once seeded, just use the list.

  gen <- newStdGen
  take 10 $ (randomRs (1,6) gen) :: [Int]

Dave



More information about the Haskell mailing list