Opened 11 years ago

Closed 11 years ago

Last modified 8 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: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:


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

Apparently, StdGen's next only returns non-negative values. Also, StdGen doesn't override de default method genRange. However, the System.Random docs ( 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 11 years ago by simonpj

Resolution: fixed
Status: newclosed

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.


comment:2 Changed 9 years ago by simonmar

Architecture: UnknownUnknown/Multiple

comment:3 Changed 9 years ago by simonmar

Operating System: UnknownUnknown/Multiple

comment:4 Changed 8 years ago by simonmar

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