Re: monadic source of randomness
Hello, Undoubtedly you can write your own monad and encapsulate the random number generation yourself. It is just an instance of a state monad. Nevertheless, the problem of using state monads for random number generation is that the code must be single-threaded, which is sometimes too restrictive -- afterall all you want is just some 'random' number. Quite a while ago I read a paper about using some techniques to encapsulate impure random number generation in a syntatic construct which looks pure. You need to pass around the seed explicitly but you get more freedom. It's not exactly what you want but you may find it interesting. F. Warren Burton and Rex L. Page. Distributed random number generation. Journal of Functional Programming, 2(2):203-212, April 1992. sincerely, Shin-Cheng Mu Norman Ramsey wrote: Does anybody know of work using monads to encapsulate a source of random numbers? Is the global random number generator, in section 17.3 of the Haskell 98 library report, the sort of thing you're after? No; that appears to embed a generator in the IO monad. Not what I'd hoped for. N"
Re: monadic source of randomness
Shin-Cheng Mu wrote: | Undoubtedly you can write your own monad and encapsulate | the random number generation yourself. It is just an | instance of a state monad. A state monad has (like you say) the disadvantage that it is single threaded. A big problem then is laziness; it becomes impossible to generate infinite structures (like trees) independently. A different approach is to define a random number generating monad in the following way: newtype R a = R (StdGen - a) instance Monad R where return x = R (\_ - x) R m1 = k = R (\rnd - let (rnd1, rnd2) = split rnd R m2 = k (m1 rnd1) in m2 rnd2) get :: R StdGen get = R (\rnd - rnd) The random seed is distributed from the top over all subcomputations. This monad is lazy enough (the order of subcomputations does not matter). However, it is not a monad! The following laws do NOT hold: return x = k === k x m = return=== m The reason for this is that `=' is the place where the random seed is distributed. A discussion of this topic can be found in [1]. In fact, the above trick can also be used for computations that need to create fresh identifiers. These kind of things do not need to be single-threaded (and often require non-single threadedness). Regards, Koen. [1] Koen Claessen and John Hughes, "QuickCheck: A Lightweight Tool for Random Testing of Haskell Programs", ICFP, Montreal, Canada, 2000. -- Koen Claessen http://www.cs.chalmers.se/~koen phone:+46-31-772 5424 mailto:[EMAIL PROTECTED] - Chalmers University of Technology, Gothenburg, Sweden
Re: monadic source of randomness
On 09-Aug-2000, Carl R. Witty [EMAIL PROTECTED] wrote: Norman Ramsey [EMAIL PROTECTED] writes: Does anybody know of work using monads to encapsulate a source of random numbers? A quick web search suggested Haskell 98 did not take this path. I'd be curious for any insights why, or any suggestions about a `randomness monad'. My guess as to why Haskell 98 does not provide a stand-alone "randomness monad" is that monads are annoying (impossible in general) to combine. Another reason is that some people favour an approach using the `Random.split' function in preference to using a monad. Using a monad imposes a sequence on things, whereas using the `Random.split' function, you can distribute a sequence of random numbers to several function calls without imposing any sequence. The resulting code is thus more symmetric and (at least in theory) more easily parallelizable. (However, little work has been done on ensuring good randomness of sequences generated using `Random.split', so if you need high quality randomness then I would not advise that approach at this point in time.) -- Fergus Henderson [EMAIL PROTECTED] | "I have always known that the pursuit WWW: http://www.cs.mu.oz.au/~fjh | of excellence is a lethal habit" PGP: finger [EMAIL PROTECTED]| -- the last words of T. S. Garp.
Re: monadic source of randomness
Does anybody know of work using monads to encapsulate a source of random numbers? Is the global random number generator, in section 17.3 of the Haskell 98 library report, the sort of thing you're after? No; that appears to embed a generator in the IO monad. Not what I'd hoped for. I'm not sure if this is what you are looking for but since I consider the IO monad overkill I use a state monad: newtype Rand a = Rand (StdGen - (a,StdGen)) Of course you don't have to use StdGen, and the monad takes care of threading the generator through your code. Generating random numbers is accomplished by rand :: (Random r) = Rand r rand = Rand random Dana
monadic source of randomness
Does anybody know of work using monads to encapsulate a source of random numbers? A quick web search suggested Haskell 98 did not take this path. I'd be curious for any insights why, or any suggestions about a `randomness monad'. Norman
monadic source of randomness
Norman Ramsey writes: Does anybody know of work using monads to encapsulate a source of random numbers? A quick web search suggested Haskell 98 did not take this path. I'd be curious for any insights why, or any suggestions about a `randomness monad'. Norman Hi. Is the global random number generator, in section 17.3 of the Haskell 98 library report, the sort of thing you're after? Regards, Tom
Re: monadic source of randomness
Does anybody know of work using monads to encapsulate a source of random numbers? Is the global random number generator, in section 17.3 of the Haskell 98 library report, the sort of thing you're after? No; that appears to embed a generator in the IO monad. Not what I'd hoped for. N
Re: monadic source of randomness
Norman Ramsey [EMAIL PROTECTED] writes: Does anybody know of work using monads to encapsulate a source of random numbers? A quick web search suggested Haskell 98 did not take this path. I'd be curious for any insights why, or any suggestions about a `randomness monad'. My guess as to why Haskell 98 does not provide a stand-alone "randomness monad" is that monads are annoying (impossible in general) to combine. If you want your own randomness monad, it's pretty easy to put a random state in a state monad; that approach means that you can easily add other chunks of state as wanted. Carl Witty