[Haskell-cafe] On the purity of Haskell /Random generators
Jerzy Karczmarczuk
jerzy.karczmarczuk at unicaen.fr
Thu Dec 29 21:39:25 CET 2011
The story begins here:
Steve Horne:
/BTW - why use an IO action for random number generation? There's a
perfectly good pure generator. It's probably handy to treat it
monadically to sequence the generator state/seed/whatever but random
number generation can be completely pure. /
===================
Bardur Arantsson:
/*Pseudo* random number generation can of course be pure (though
threading the state would be tedious and error-prone). If you want
truly random numbers you cannot avoid IO (the monad). /
??? ??????? :
> If seed depends of psudo random event, for example of current systime,
> we must use IO action.
Bardur, Lev, of course you try to be helpful, but you are answering
wrong questions.
"Truly random" numbers are very rarely used, forget about them.
"Standard" r. generators (pseudo-random) in Haskell are monadic, because
the relevant algorithms are stateful.
Congruential, Fibonacci, Mersenne Twister, whatever, is a function, more
or less:
(newValue,newSeed) = rgen seed
The monadic approach serves mainly to hide the seed.
Some people prefer to use random streams, no monads, so the question of
Steve Horne is not universal.
But anyway, even without bind, the user will have to chain the seeds in
some way.
Still, I dont understand what does S.H. mean by a "perfectly good pure
generator".
Tell more please (unless you just mean a stream, say:
str = map fst (iterate (\(v,sd) -> rgen sd) (v0,sd0))
)
Jerzy
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20111229/45f19ded/attachment.htm>
More information about the Haskell-Cafe
mailing list