[Haskell-cafe] How to use randomized algorithm within the implementation of pure data structures?

Hiromi ISHII konn.jinro at gmail.com
Sun Nov 2 06:35:59 UTC 2014


Hi there,

Thank you in advance for many people suggesting solution for this problem!

On 2014/11/01 20:59, Travis Cardwell <travis.cardwell at extellisys.com> wrote:
> If it works with the algorithm, you could use a pseudo-random number
> generator with a fixed seed.  For example, here is a program to estimate

> the value of π (purely) using a Monte Carlo simulation:

I got it. But, in my case, fixing initial seed might cause inefficiency, so I can't take this way in this case.
By the way, this approach seems works well for other cases which is not so quality sensitive.


On 2014/11/02 1:02, Carter Schonwald <carter.schonwald at gmail.com> wrote:

> Hrm, you could make a num instance of a newtype wrapped state monad thats threading around your math!
> 
> newtype MyNum =  MN (State StdGen TheNumberType)

This is almost the same as what I mean by "CPS-ing". We can omit generator argument by this approach,
but we have to pass generator whenever we want to get the result or inspect the intermediate value.


On 2014/11/02 1:15, Jun Inoue <jun.lambda at gmail.com> wrote:

> Just an idea here, but would implicit-params work?  It only gives you
> Reader-monad capabilities, but you can always split random generators.
> There might be repercussions for the quality of the generated numbers,
> though, for which I have no idea.

I think this is virtually the same as Travis's approach, because it returns same seed whenever we split the global value.
And, yes, this causes repercussions for the random quality, and not suitable for my case.
Perhaps we can parametrize `IORef StdGen` instead of `StdGen` so that we can change the state,
but we have to call `unsafePerformIO` internally whenever accessing random generator, 
and then this appoarch became almost the same as my initial approach.
By the way, we can control initial value with this implicit parameter approach, this might improve the random quality
slightly than splitting global generator beforewards.


-- Hiromi ISHII
konn.jinro at gmail.com



-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 496 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20141102/6695f17d/attachment.sig>


More information about the Haskell-Cafe mailing list