haskell / mwc-random

A very fast Haskell library for generating high quality pseudo-random numbers.
http://hackage.haskell.org/package/mwc-random
BSD 2-Clause "Simplified" License
54 stars 25 forks source link

Add a pure API with explicit random state passing #49

Closed sdroege closed 8 years ago

sdroege commented 9 years ago

It would be great if mwc-random had a pure API that explictly passes random state around, especially for integration with something like MonadRandom ( https://hackage.haskell.org/package/MonadRandom ).

Shimuuar commented 9 years ago

I don't think it's good idea. State is rather large: 258 Word32 so it's not efficient to carry it around.And implementation uses in-place state mutation to achieve high performance

sdroege commented 9 years ago

That's actually a good point, didn't think of that. Might maybe make more sense for my use case to build a new MonadRandom-like thing that runs in PrimMonad

Shimuuar commented 9 years ago

Writing generic API for random number generation is challenging task. All PRNG I know about (I'm no expert though) generate uniformly distributed bit vector (usually 32 or 64 bits wide). Everything else is built upon it

Haskell PRNGs could be split into two main categories: ones with pure state (System.Random), and ones with mutable state. Latter tend to have large state (mwc8222, Mersenne twister). Explicit state passing or passing of reference to mutable state could be abstracted using state or reader monad correspondingly. Unifying API this disparate is not easy task

sdroege commented 9 years ago

FWIW, I went with this solution for my own uses now: https://gist.github.com/sdroege/6209e97b4dfc9791033d Might be useful to have something like this in mwc-random

Shimuuar commented 9 years ago

You might be interested in mwc-random-monad package. It provides monadic API for mwc-random but doesn't generalize different generators

sdroege commented 9 years ago

Ah, for some reason I didn't find that package and reinvented parts of it :)

Shimuuar commented 8 years ago

I'm closing this issue.