purescript-jack icon indicating copy to clipboard operation
purescript-jack copied to clipboard

Strange behavior of random number generator

Open zyla opened this issue 7 years ago • 1 comments

The boundedInt generator returns the same value (2147483647, 0x7fffffff) for seeds lower than -84 or higher than 104. The following program demonstrates the issue:

module Example where

import Prelude
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log)
import Data.Array (range)
import Data.Foldable (for_)
import Jack (boundedInt, outcome, runGen)
import Jack.Random (runRandom)
import Jack.Seed (mkSeed)

main :: Eff (console :: CONSOLE) Unit
main = do
  let seeds = range (-90) (-80) <> range 100 110
  for_ seeds $ \seed ->
    let
      int = runRandom (mkSeed seed) 1 $ map outcome $ runGen boundedInt
    in
      log $ show seed <> " -> " <> show int

Output on my machine:

-90 -> 2147483647
-89 -> 2147483647
-88 -> 2147483647
-87 -> 2147483647
-86 -> 2147483647
-85 -> 2147483647
-84 -> -3285845
-83 -> -6687035
-82 -> -10088225
-81 -> -13489415
-80 -> -16890605
100 -> -340061285
101 -> -343462475
102 -> -346863665
103 -> -350264855
104 -> -353666045
105 -> 2147483647
106 -> 2147483647
107 -> 2147483647
108 -> 2147483647
109 -> 2147483647
110 -> 2147483647

Quick investigation using Debug.Trace suggests some Int53 overflow inside Jack.Seed.Int53, but I don't have time to dig further.

zyla avatar Feb 21 '18 23:02 zyla

By the way, thanks for making this awesome library! It's a very useful piece of the PureScript ecosystem, because purescript-quickcheck doesn't have shrinking.

zyla avatar Feb 21 '18 23:02 zyla