10000 Allow multiple versions of RandomState: init' work. by anntzer · Pull Request #5911 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

Allow multiple versions of RandomState: init' work. #5911

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 10 commits into from
Prev Previous commit
Fix for Python2 and 32bit builds.
  • Loading branch information
anntzer committed Jun 26, 2015
commit ef9ad3415d7de2b7d58a2847946bb76c2691f653
6 changes: 3 additions & 3 deletions numpy/random/mtrand/mtrand.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ cdef class RandomState:
if version is None:
version = _HIGHEST_VERSION if seed is None else 0
if version not in range(_HIGHEST_VERSION + 1):
raise ValueError("`version` must be an integer between 0 and {}".
raise ValueError("`version` must be an integer between 0 and {0}".
format(_HIGHEST_VERSION))
self.version = version

Expand Down Expand Up @@ -777,13 +777,13 @@ cdef class RandomState:
"""
cdef ndarray obj "arrayObject_obj"
cdef int pos
if isinstance(state[0], str):
if isinstance(state[0], basestring):
version = 0
rng, *rest = state
else:
version, rng, *rest = state
if version not in range(_HIGHEST_VERSION + 1):
raise ValueError("`version` must be an integer between 0 and {}".
raise ValueError("`version` must be an integer between 0 and {0}".
format(_HIGHEST_VERSION))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error should be split in two, to distinguish "this is not a valid version" and "this version is newer than this version of numpy"

if rng != 'MT19937':
raise ValueError("rng must be 'MT19937'")
Expand Down
Binary file modified numpy/random/tests/randomstate.pkl
Binary file not shown.
6 changes: 3 additions & 3 deletions numpy/random/tests/test_random.py
5001
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,15 @@ def test_backwards_compatibility_version(self):
assert_(isinstance(state[1], str))

def test_version_bounds(self):
version, *rest = RandomState(version=1).get_state()
state_without_version = RandomState(version=1).get_state()[1:]
assert_raises(ValueError, RandomState().set_state,
(random.HIGHEST_VERSION + 1,) + tuple(rest))
(random.HIGHEST_VERSION + 1,) + state_without_version)

def test_backwards_compatibility_pickle(self):
# Pickled with numpy 1.9.2.
with open(os.path.join(os.path.dirname(__file__), "randomstate.pkl"),
"rb") as f:
assert_(pickle.load(f).tomaxint() == 7272208372766828204)
assert_(pickle.load(f).randint(2 ** 31 - 1) == 103801538)

def test_negative_binomial(self):
# Ensure that the negative binomial results take floating point
Expand Down
0