-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
BUG: Fix nonrandom first uint32 in MT19937 state initialization. #14847
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
I suppose this would be rather tricky to test except by taking a large number of samples. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic is still wrong.
For testing, you can give it a |
Or rather, a variety of values, in a controlled fashion. |
|
Also, don't forget to fix the other instance of this bug in |
tests added for both fixes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like there is also this bug in mt19937_init_by_array
, which is likely the original typo that got translated into the other code paths. It's only used in the legacy seeding for RandomState
[~]
|4> np.random.RandomState([10, 20, 30]).get_state()[1][0]
2147483648
[~]
|5> np.random.RandomState([10, 20, 31]).get_state()[1][0]
2147483648
[~]
|6> np.random.RandomState([10, 20, 32]).get_state()[1][0]
2147483648
[~]
|8> np.random.MT19937([10, 20, 30]).state['state']['key'][0]
3742924469
[~]
|9> np.random.MT19937([10, 20, 31]).state['state']['key'][0]
2480826684
[~]
|10> np.random.MT19937([10, 20, 32]).state['state']['key'][0]
3174699929
@rkern tested and fixed for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Thanks for seeing this through!
This will definitely require some release notes.
Was this also a problem with the legacy code in 1.16.x? EDIT: Looks like yes. Is it worth backporting to 1.16.x? |
No, all this was introduced in 1.17 |
There is this in 1.16.x
|
Huh. Okay. So I guess we ought to revert that one to maintain our stream-compatibility guarantees. |
That one dates all the way back. It's still that way in Python. My apologies. I knew that |
Do we have legacy-seeding stream tests for the array-seed initialization in addition to the integer-seed route? We probably should. |
Only |
Yes. Let's add some stream tests to be sure. |
Hmm, seems like |
Ah! The lower bits of the first word in the key never actually participate in the algorithm! numpy/numpy/random/src/mt19937/mt19937.c Lines 87 to 90 in 89f30cb
Nothing was ever broken! It just looked distressing to the user. We can just close this, if we want. Sorry, @mattip! |
Fixes gh-14844. As noted in the comments there by @rkern, the bug was to set the first byte to a value rather than to OR it with that value.