Need help

Alastair Reid alastair@reid-consulting-uk.ltd.uk
23 Jul 2002 18:26:59 +0100


> You shouldn't _need_ to be in the IO monad to get random numbers
> (although if you choose to that can be a good choice). Clearly
> there's the need to initialise the generator, but if you want
> `random' random numbers (as opposed to a known sequence of random
> numbers for debugging) getting the value of time via an
> unsafePerformIO is as good as anything else. From then on, the
> pseudo-random number generator will deterministically produce what
> are hopefully `acceptably random looking' numbers.

Isn't this a very dangerous practice?  It's so very, very easy to
break referential transparency when using unsafePerformIO with
functions known to produce observably different results each time you
call it.  And once you do this, all kinds of nice Haskell properties
go to hell.

Safer ways would be to use the monadic operators as intended to get
random seeds and then use implicit parameters to pass them around
(using a mild variation of John Hughes' approach to mutable
variables).

--
Alastair Reid                 alastair@reid-consulting-uk.ltd.uk  
Reid Consulting (UK) Limited  http://www.reid-consulting-uk.ltd.uk/alastair/