summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Sheppard <kevin.k.sheppard@gmail.com>2017-10-09 14:57:38 +0100
committerKevin Sheppard <kevin.k.sheppard@gmail.com>2017-10-18 11:43:47 +0100
commitad8a4c72321dcdde4ead76a0dc2b67071e75326a (patch)
tree71a6586a899aa7f170a264d42f7f797a42d17d19
parent77f9540f277de3e727d696eb5987a0505ed8cdf9 (diff)
downloadpython-numpy-ad8a4c72321dcdde4ead76a0dc2b67071e75326a.tar.gz
python-numpy-ad8a4c72321dcdde4ead76a0dc2b67071e75326a.tar.bz2
python-numpy-ad8a4c72321dcdde4ead76a0dc2b67071e75326a.zip
BUG: Prevent invalid array shapes in seed
Prevent empty arrays or arrays with more than 1 dimension from being used to seed RandomState closes #9832
-rw-r--r--doc/release/1.14.0-notes.rst6
-rw-r--r--numpy/random/mtrand/mtrand.pyx15
-rw-r--r--numpy/random/tests/test_random.py7
3 files changed, 23 insertions, 5 deletions
diff --git a/doc/release/1.14.0-notes.rst b/doc/release/1.14.0-notes.rst
index 5dbd9b44c..24a9e9bef 100644
--- a/doc/release/1.14.0-notes.rst
+++ b/doc/release/1.14.0-notes.rst
@@ -369,3 +369,9 @@ display the sign. This new behavior can be disabled to mostly reproduce numpy
-----------------------------------------------------------------
These options could previously be controlled using ``np.set_printoptions``, but
now can be changed on a per-call basis as arguments to ``np.array2string``.
+
+Seeding ``RandomState`` using an array requires a 1-d array
+-----------------------------------------------------------
+``RandomState`` previously would accept empty arrays or arrays with 2 or more
+dimensions, which resulted in either a failure to seed (empty arrays) or for
+some of the passed values to be ignored when setting the seed.
diff --git a/numpy/random/mtrand/mtrand.pyx b/numpy/random/mtrand/mtrand.pyx
index adf820f0d..bf6d7e95a 100644
--- a/numpy/random/mtrand/mtrand.pyx
+++ b/numpy/random/mtrand/mtrand.pyx
@@ -659,7 +659,7 @@ cdef class RandomState:
Parameters
----------
- seed : int or array_like, optional
+ seed : int or 1-d array_like, optional
Seed for `RandomState`.
Must be convertible to 32 bit unsigned integers.
@@ -676,14 +676,19 @@ cdef class RandomState:
errcode = rk_randomseed(self.internal_state)
else:
idx = operator.index(seed)
- if idx > int(2**32 - 1) or idx < 0:
+ if (idx >= 2**32) or (idx < 0):
raise ValueError("Seed must be between 0 and 2**32 - 1")
with self.lock:
rk_seed(idx, self.internal_state)
except TypeError:
- obj = np.asarray(seed).astype(np.int64, casting='safe')
- if ((obj > int(2**32 - 1)) | (obj < 0)).any():
- raise ValueError("Seed must be between 0 and 2**32 - 1")
+ obj = np.asarray(seed)
+ if obj.size == 0:
+ raise ValueError("Seed must be non-empty")
+ obj = obj.astype(np.int64, casting='safe')
+ if obj.ndim != 1:
+ raise ValueError("Seed array must be 1-d")
+ if ((obj >= 2**32) | (obj < 0)).any():
+ raise ValueError("Seed values must be between 0 and 2**32 - 1")
obj = obj.astype('L', casting='unsafe')
with self.lock:
init_by_array(self.internal_state, <unsigned long *>PyArray_DATA(obj),
diff --git a/numpy/random/tests/test_random.py b/numpy/random/tests/test_random.py
index e9c9bc492..6ada4d997 100644
--- a/numpy/random/tests/test_random.py
+++ b/numpy/random/tests/test_random.py
@@ -42,6 +42,13 @@ class TestSeed(object):
assert_raises(ValueError, np.random.RandomState, [1, 2, 4294967296])
assert_raises(ValueError, np.random.RandomState, [1, -2, 4294967296])
+ def test_invalid_array_shape(self):
+ # gh-9832
+ assert_raises(ValueError, np.random.RandomState, np.array([], dtype=np.int64))
+ assert_raises(ValueError, np.random.RandomState, [[1, 2, 3]])
+ assert_raises(ValueError, np.random.RandomState, [[1, 2, 3],
+ [4, 5, 6]])
+
class TestBinomial(object):
def test_n_zero(self):