10000 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 all commits
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
8 changes: 4 additions & 4 deletions numpy/random/_mt19937.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ cdef class MT19937(BitGenerator):
BitGenerator.__init__(self, seed)
val = self._seed_seq.generate_state(RK_STATE_LEN, np.uint32)
# MSB is 1; assuring non-zero initial array
self.rng_state.key[0] = 0x80000000UL
for i in range(1, RK_STATE_LEN):
for i in range(RK_STATE_LEN):
self.rng_state.key[i] = val[i]
self.rng_state.key[0] |= 0x80000000UL
self.rng_state.pos = i

self._bitgen.state = &self.rng_state
Expand Down Expand Up @@ -169,9 +169,9 @@ cdef class MT19937(BitGenerator):
seed = SeedSequence()
val = seed.generate_state(RK_STATE_LEN)
# MSB is 1; assuring non-zero initial array
self.rng_state.key[0] = 0x80000000UL
for i in range(1, RK_STATE_LEN):
for i in range(RK_STATE_LEN):
self.rng_state.key[i] = val[i]
self.rng_state.key[0] |= 0x80000000UL
else:
if hasattr(seed, 'squeeze'):
seed = seed.squeeze()
Expand Down
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
22 changes: 21 additions & 1 deletion numpy/random/tests/test_generator_mt19937.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ def test_noninstantized_bitgen(self):
assert_raises(ValueError, Generator, MT19937)


def test_seed_init(self):
# gh-14844. seed[0] was not correctly initialized
# key[0] always held the same value

class FullSeedSequence(SeedSequence):

def __init__(self, x):
self.x = x

def generate_state(self, n_words, dtype=np.uint32):
return np.full(n_words, self.x, dtype=dtype)

for i in range(32):
ss = FullSeedSequence(1 << i)
key = MT19937(ss).state['state']['key']
expected = np.full(624, 1 << i, dtype=np.uint32)
expected[0] |= 0x80000000
np.testing.assert_array_equal(key, expected)


class TestBinomial(object):
def test_n_zero(self):
# Tests the corner case of n == 0 for the binomial distribution.
Expand Down Expand Up @@ -944,7 +964,7 @@ def test_permutation(self):
arr_2d = np.atleast_2d([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]).T
actual = random.permutation(arr_2d)
assert_array_equal(actual, np.atleast_2d(desired).T)

bad_x_str = "abcd"
assert_raises(np.AxisError, random.permutation, bad_x_str)

Expand Down
21 changes: 20 additions & 1 deletion numpy/random/tests/test_randomstate.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,25 @@ def test_cannot_seed(self):
def test_invalid_initialization(self):
assert_raises(ValueError, random.RandomState, MT19937)

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']
res = np.empty((32,) + key.shape, dtype=key.dtype)
for i in range(32):
# enter the _legacy_seed(None) path
rs.seed()
key = rs._bit_generator.state['state']['key']
res[i, ...] = key
res.shape = (32, -1)
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):
def test_n_zero(self):
Expand Down Expand Up @@ -618,7 +637,7 @@ def test_choice_nan_probabilities(self):
a = np.array([42, 1, 2])
p = [None, None, None]
assert_raises(ValueError, random.choice, a, p=p)

def test_choice_p_non_contiguous(self):
p = np.ones(10) / 5
p[1::2] = 3.0
Expand Down
0