Opened 9 years ago

Closed 9 years ago

Last modified 6 years ago

#794 closed bug (fixed)

System.Random: StdGen's genRange doesn't match its next

Reported by: rturk@… Owned by:
Priority: normal Milestone:
Component: libraries/base Version: 6.4.2
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Test Case:
Blocked By: Blocking:
Related Tickets: Differential Revisions:

Description

> g <- getStdGen
> length $ filter (<0) $ take (10^6) $ unfoldr (Just . next) g
0
> genRange g
(-2147483648,2147483647)

Apparently, StdGen's next only returns non-negative values. Also, StdGen doesn't override de default method genRange. However, the System.Random docs (http://haskell.org/ghc/docs/latest/html/libraries/base/System-Random.html#v%3Anext) promise:

The next operation returns an Int that is uniformly distributed in the range returned by genRange (including both end points), and a new generator.

Thus, StdGen's next violates the uniform distribution requirement.

Change History (4)

comment:1 Changed 9 years ago by simonpj

  • Resolution set to fixed
  • Status changed from new to closed

Good point. Rather than try to make Random.stdNext return numbers in the full range (which is dodgy, because stdNext is carefully crafted code), I changed the genRange method of Random StdGen to return the range that stdNext actually delivers.

The documentation only guarantees a 30-bit range, which is still the case.

Simon

comment:2 Changed 7 years ago by simonmar

  • Architecture changed from Unknown to Unknown/Multiple

comment:3 Changed 7 years ago by simonmar

  • Operating System changed from Unknown to Unknown/Multiple

comment:4 Changed 6 years ago by simonmar

  • difficulty changed from Easy (1 hr) to Easy (less than 1 hour)
Note: See TracTickets for help on using tickets.