haskell / random

Random number library
Other
53 stars 50 forks source link

Fix doctests #165

Closed sol closed 1 month ago

sol commented 1 month ago

This addresses https://github.com/commercialhaskell/stackage/issues/7493.

idontgetoutmuch commented 1 month ago

Why do the generated random numbers change? I would have thought with a fixed seed we should always get the same random numbers i.e. why this: https://github.com/haskell/random/pull/165/files#diff-fe93ae678c398cfea72ff60186cc2cee27bd752aad99d8fd1ece059c912f0104L645

sol commented 1 month ago

@idontgetoutmuch I don't think the seed was fixed for those examples before this PR.

lehins commented 1 month ago

Why do the generated random numbers change? I would have thought with a fixed seed we should always get the same random numbers i.e. why this: https://github.com/haskell/random/pull/165/files#diff-fe93ae678c398cfea72ff60186cc2cee27bd752aad99d8fd1ece059c912f0104L645

@idontgetoutmuch That is because that example uses global StdGen, which as comment below example points out, is usually not a very good idea:

https://github.com/haskell/random/blob/623cf518c6bc4c1de6a4d59b43c3ae37fcb22a35/src/System/Random.hs#L647-L650

Shimuuar commented 1 month ago

In fact we do fix seed for doctests. In module System.Random.Stateful:

-- $setup
-- >>> import Control.Monad.Primitive
-- >>> import qualified System.Random.MWC as MWC
-- >>> writeIORef theStdGen $ mkStdGen 2021

Maybe cabal doctest executes modules in different order?

lehins commented 1 month ago

Maybe cabal doctest executes modules in different order?

I am sure that is the case. Which is also the reason why I always suggest avoid using the global generator whenever reproducability is important.