Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MonadRandom IO instance on Windows #32

Open
cmears opened this issue Mar 15, 2015 · 3 comments
Open

MonadRandom IO instance on Windows #32

cmears opened this issue Mar 15, 2015 · 3 comments

Comments

@cmears
Copy link

cmears commented Mar 15, 2015

Hello,

This test program works well on Linux but not on Windows. I am using the library the right way?

import Data.Random.Sample
import Data.Random.Distribution.Uniform
import Data.Random.Source.IO
import Control.Monad

main = print =<< replicateM 10000 (sample (uniform 0 10) :: IO Int)

On Linux I get a random-looking sequence, but on Windows I get big chunks of the same number repeated.

I think the problem is in the instance of MonadRandom:

instance MonadRandom IO where
    getRandomPrim = withSystemRandom . (flip getRandomPrimFrom :: Prim t -> Gen RealWorld -> IO t)

This seems to call withSystemRandom for every random number, which reseeds the generator, and on Windows the current time is used as the seed, so every call within the same second gets the same result. (The documentation for withSystemRandom says that it should only be called once per thread or so [https://github.com/bos/mwc-random/blob/master/System/Random/MWC.hs#L423].)

@idontgetoutmuch
Copy link
Member

Very interesting. It will take me some time before I can look at this. I trust you have found a workaround for what you want to do? If not let me know and I can make some suggestions.

@cmears
Copy link
Author

cmears commented Mar 16, 2015

Yes, I've worked around it by creating a PureMT explicitly, putting it in an IORef, passing the IORef where needed and using sampleFrom directly. The other way might have been a bit neater, but it's not a big problem. I was definitely surprised by the odd behaviour, though!

@eflister
Copy link

same problem here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants