You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Similar periodicity shows up with various static seeds and MT19937; flipping the use_masked flag in random_bounded_uint8_fill() results in all values having approximately equal frequencies.
Same for 1.17 and 1.18.0.dev0+c10d66a 3.7.3 (default, Oct 7 2019, 12:56:13) [GCC 8.3.0].
A work-around is to not use the dtype argument, and instead use the astype() method to cast the values to the desired data type after they are generated, e.g.:
The problem appears to be a bug in the implementation of Lemire's algorithm for random integers in an interval. Once I've confirmed the problem, I'll submit a PR with a fix.
@bashtage, add use_masked=False to your calls to integers. I'm comparing randomgen to what is currently in NumPy, and it looks like the default in randomgen doesn't call the Lemire code.
Activity
WarrenWeckesser commentedon Oct 24, 2019
It isn't just the
int8dtype. Biased results are also generated withdtype=np.int16.Here's a variation of your script that illustrates a problem when
dtype=np.int16:It generates this plot:

A work-around is to not use the
dtypeargument, and instead use theastype()method to cast the values to the desired data type after they are generated, e.g.:charris commentedon Oct 24, 2019
Looks like a regression. ISTR that the new routines use a different algorithm for generating bounded integers. @bashtage @rkern.
WarrenWeckesser commentedon Oct 24, 2019
The problem appears to be a bug in the implementation of Lemire's algorithm for random integers in an interval. Once I've confirmed the problem, I'll submit a PR with a fix.
bashtage commentedon Oct 24, 2019
This is not an issue in randomgen, which suggests something happened in the port over.
bashtage commentedon Oct 24, 2019
The implementation in randomgen has deviated a bit from what is here since there is more separation between the masked and the lemire generation code.
bashtage commentedon Oct 24, 2019
bashtage commentedon Oct 24, 2019
Other example
produces:
WarrenWeckesser commentedon Oct 24, 2019
@bashtage, add
use_masked=Falseto your calls tointegers. I'm comparingrandomgento what is currently in NumPy, and it looks like the default inrandomgendoesn't call the Lemire code.WarrenWeckesser commentedon Oct 24, 2019
The bug is this line in the implementation of the Lemire algorithm:
-rng_excl % rng_exclis not computing the desired mod. In this case, where the type isuint8_t, the result is 0 for any value ofrng_excl.bashtage commentedon Oct 24, 2019
Yes, I see it now.
orange is
use_masked=FalseWarrenWeckesser commentedon Oct 25, 2019
Proposed fix is in #14777.
BUG: random: biased samples from integers() with 8 or 16 bit dtype.
Bump NumPy to 1.18.4
Bump NumPy to 1.18.4
Bump NumPy to 1.18.4