[GHC] #8899: StdGen does not generate 0

GHC ghc-devs at haskell.org
Sat Mar 15 15:57:00 UTC 2014


#8899: StdGen does not generate 0
------------------------------------+-------------------------------------
       Reporter:  novadenizen       |             Owner:
           Type:  bug               |            Status:  new
       Priority:  normal            |         Milestone:
      Component:  libraries/random  |           Version:  7.9
       Keywords:                    |  Operating System:  Unknown/Multiple
   Architecture:  Unknown/Multiple  |   Type of failure:  Other
     Difficulty:  Unknown           |         Test Case:
     Blocked By:                    |          Blocking:
Related Tickets:                    |
------------------------------------+-------------------------------------
 `genRange` for `StdGen` returns `(0,2147483562)`.  However, as far as I
 can tell, `StdGen` doesn't generate 0.

 This code performs 200 billion iterations of `next` on a `StdGen`.  I ran
 it and it output `Nothing`.  The probability that no 0 was generated by
 chance is approximately ''e^-200/2.147^'' =~ ''10^-40^''.

 {{{#!haskell
 import System.Random
 import Data.Int

 find0 :: StdGen -> Int64 -> Maybe Int64
 find0 g0 n0 = aux g0 n0 where
     aux _ 0 = Nothing
     aux g r = let (v,g') = next g in
                   if v == 0 then Just (n0 - r + 1)
                             else aux g' (r-1)

 main :: IO ()
 main = print $ find0 (mkStdGen 1234) 200000000000
   }}}

--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/8899>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler


More information about the ghc-tickets mailing list