summaryrefslogtreecommitdiff
path: root/numpy/random/mtrand
diff options
context:
space:
mode:
Diffstat (limited to 'numpy/random/mtrand')
-rw-r--r--numpy/random/mtrand/mtrand.pyx11
-rw-r--r--numpy/random/mtrand/numpy.pxd2
2 files changed, 11 insertions, 2 deletions
diff --git a/numpy/random/mtrand/mtrand.pyx b/numpy/random/mtrand/mtrand.pyx
index 3f9dcb687..c2603543d 100644
--- a/numpy/random/mtrand/mtrand.pyx
+++ b/numpy/random/mtrand/mtrand.pyx
@@ -628,6 +628,7 @@ cdef class RandomState:
----------
seed : int or array_like, optional
Seed for `RandomState`.
+ Must be convertable to 32 bit unsigned integers.
See Also
--------
@@ -640,9 +641,15 @@ cdef class RandomState:
if seed is None:
errcode = rk_randomseed(self.internal_state)
else:
- rk_seed(operator.index(seed), self.internal_state)
+ idx = operator.index(seed)
+ if idx > int(2**32 - 1) or idx < 0:
+ raise ValueError("Seed must be between 0 and 4294967295")
+ rk_seed(idx, self.internal_state)
except TypeError:
- obj = <ndarray>PyArray_ContiguousFromObject(seed, NPY_LONG, 1, 1)
+ 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 4294967295")
+ obj = obj.astype('L', casting='unsafe')
init_by_array(self.internal_state, <unsigned long *>PyArray_DATA(obj),
PyArray_DIM(obj, 0))
diff --git a/numpy/random/mtrand/numpy.pxd b/numpy/random/mtrand/numpy.pxd
index 6812cc164..c54f79c0a 100644
--- a/numpy/random/mtrand/numpy.pxd
+++ b/numpy/random/mtrand/numpy.pxd
@@ -121,6 +121,8 @@ cdef extern from "numpy/arrayobject.h":
object PyArray_IterNew(object arr)
void PyArray_ITER_NEXT(flatiter it) nogil
+ dtype PyArray_DescrFromType(int)
+
void import_array()
# include functions that were once macros in the new api