8000 BUG: Fix nonrandom first uint32 in MT19937 state initialization. by mattip · Pull Request #14847 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
BUG: test, fix mt19937_init_by_array
  • Loading branch information
mattip committed Nov 7, 2019
commit 89f30cbc4c0da2cd88e096d1935f8b9548a8b180
2 changes: 1 addition & 1 deletion numpy/random/src/mt19937/mt19937.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void mt19937_init_by_array(mt19937_state *state, uint32_t *init_key,
}
}

mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */
mt[0] |= 0x80000000UL; /* MSB is 1; assuring non-zero initial array */
}

void mt19937_gen(mt19937_state *state) {
Expand Down
14 changes: 8 additions & 6 deletions numpy/random/tests/test_randomstate.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ def test_cannot_seed(self):
def test_invalid_initialization(self):
assert_raises(ValueError, random.RandomState, MT19937)

def test_init_unique(self):
# gh-14844. seed[0] was not correctly initialized;
def test_reseed_unique(self):
# gh-14844. seed[0] was not correctly re-initialized in legacy seed()
# key[0] always held the same value
rs = random.RandomState()
key = rs._bit_generator.state['state']['key']
Expand All @@ -118,11 +118,13 @@ def test_init_unique(self):
rs.seed()
key = rs._bit_generator.state['state']['key']
res[i, ...] = key
# make sure each the corresponding values at each position in key
# are unique. The chances of that failing should be very low.
res.shape = (32, -1)
for i in range(res.shape[1]):
assert len(np.unique(res[:, i])) == 32
assert not (res[:, 0] == 0x80000000).all()

rs.seed(np.ones(100, dtype='int64'))
k1 = rs._bit_generator.state['state']['key']
assert k1[0] != 0x80000000



class TestBinomial(object):
Expand Down
0