[Pharo-dev] atRandom & integer agnosticism

Henrik Sperre Johansen henrik.s.johansen at veloxit.no
Mon Feb 11 07:23:40 EST 2019


The "random" index for atRandom is, basically, computed prngOutput mod:
selectionRange.

For large selection ranges, it fails because the period of the PRNG used by
atRandom is 2^30th, with the output an integer in the range 1 - 2^30th. (in
other words, the prng is a repeating sequence of every number between 1 and
2^30th)

That means: 
- There will be bias when the range you pick from approaches 2^30th. 
- There will be gaps (indexes that won't ever be picked) once you pass
2^30th.

The reason is techical; the default RNG is a reasonable compromise between
speed, memory, and usable range.
If the intended use requires a larger range, an appropriate random number
generator can be provided using atRandom: 
It certainly could give an error and/or select another RNG automatically if
outside applicable range, but no one has been bothered enough to do either,
yet.

Cheers,
Henry



--
Sent from: http://forum.world.st/Pharo-Smalltalk-Developers-f1294837.html



More information about the Pharo-dev mailing list