summaryrefslogtreecommitdiff
path: root/numpy/random
diff options
context:
space:
mode:
authorCharles Harris <charlesr.harris@gmail.com>2020-12-13 14:14:49 -0700
committerGitHub <noreply@github.com>2020-12-13 14:14:49 -0700
commit3fe2d9d2627fc0f84aeed293ff8afa7c1f08d899 (patch)
tree2ea27fe06a19c39e8d7a5fe2f87cb7e05363247d /numpy/random
parent7d7e446fcbeeff70d905bde2eb0264a797488280 (diff)
parenteff302e5e8678fa17fb3d8156d49eb585b0876d9 (diff)
downloadnumpy-3fe2d9d2627fc0f84aeed293ff8afa7c1f08d899.tar.gz
Merge branch 'master' into fix-issue-10244
Diffstat (limited to 'numpy/random')
-rw-r--r--numpy/random/__init__.pyi61
-rw-r--r--numpy/random/_common.pxd2
-rw-r--r--numpy/random/_examples/cython/setup.py4
-rw-r--r--numpy/random/_examples/numba/extending.py4
-rw-r--r--numpy/random/_generator.pyx315
-rw-r--r--numpy/random/bit_generator.pxd2
-rw-r--r--numpy/random/mtrand.pyx86
-rw-r--r--numpy/random/setup.py1
-rw-r--r--numpy/random/src/pcg64/pcg64.h6
-rw-r--r--numpy/random/tests/test_direct.py4
-rw-r--r--numpy/random/tests/test_generator_mt19937.py110
-rw-r--r--numpy/random/tests/test_generator_mt19937_regressions.py4
-rw-r--r--numpy/random/tests/test_random.py24
-rw-r--r--numpy/random/tests/test_randomstate.py74
-rw-r--r--numpy/random/tests/test_randomstate_regression.py4
-rw-r--r--numpy/random/tests/test_regression.py4
-rw-r--r--numpy/random/tests/test_smoke.py9
17 files changed, 517 insertions, 197 deletions
diff --git a/numpy/random/__init__.pyi b/numpy/random/__init__.pyi
new file mode 100644
index 000000000..f7c3cfafe
--- /dev/null
+++ b/numpy/random/__init__.pyi
@@ -0,0 +1,61 @@
+from typing import Any
+
+beta: Any
+binomial: Any
+bytes: Any
+chisquare: Any
+choice: Any
+dirichlet: Any
+exponential: Any
+f: Any
+gamma: Any
+geometric: Any
+get_state: Any
+gumbel: Any
+hypergeometric: Any
+laplace: Any
+logistic: Any
+lognormal: Any
+logseries: Any
+multinomial: Any
+multivariate_normal: Any
+negative_binomial: Any
+noncentral_chisquare: Any
+noncentral_f: Any
+normal: Any
+pareto: Any
+permutation: Any
+poisson: Any
+power: Any
+rand: Any
+randint: Any
+randn: Any
+random: Any
+random_integers: Any
+random_sample: Any
+ranf: Any
+rayleigh: Any
+sample: Any
+seed: Any
+set_state: Any
+shuffle: Any
+standard_cauchy: Any
+standard_exponential: Any
+standard_gamma: Any
+standard_normal: Any
+standard_t: Any
+triangular: Any
+uniform: Any
+vonmises: Any
+wald: Any
+weibull: Any
+zipf: Any
+Generator: Any
+RandomState: Any
+SeedSequence: Any
+MT19937: Any
+Philox: Any
+PCG64: Any
+SFC64: Any
+default_rng: Any
+BitGenerator: Any
diff --git a/numpy/random/_common.pxd b/numpy/random/_common.pxd
index 588f613ae..4f404b7a1 100644
--- a/numpy/random/_common.pxd
+++ b/numpy/random/_common.pxd
@@ -77,6 +77,8 @@ cdef object wrap_int(object val, object bits)
cdef np.ndarray int_to_array(object value, object name, object bits, object uint_size)
+cdef validate_output_shape(iter_shape, np.ndarray output)
+
cdef object cont(void *func, void *state, object size, object lock, int narg,
object a, object a_name, constraint_type a_constraint,
object b, object b_name, constraint_type b_constraint,
diff --git a/numpy/random/_examples/cython/setup.py b/numpy/random/_examples/cython/setup.py
index 42425c2c1..83f06fde8 100644
--- a/numpy/random/_examples/cython/setup.py
+++ b/numpy/random/_examples/cython/setup.py
@@ -19,7 +19,7 @@ inc_path = np.get_include()
lib_path = join(np.get_include(), '..', '..', 'random', 'lib')
extending = Extension("extending",
- sources=[join(path, 'extending.pyx')],
+ sources=[join('.', 'extending.pyx')],
include_dirs=[
np.get_include(),
join(path, '..', '..')
@@ -27,7 +27,7 @@ extending = Extension("extending",
define_macros=defs,
)
distributions = Extension("extending_distributions",
- sources=[join(path, 'extending_distributions.pyx')],
+ sources=[join('.', 'extending_distributions.pyx')],
include_dirs=[inc_path],
library_dirs=[lib_path],
libraries=['npyrandom'],
diff --git a/numpy/random/_examples/numba/extending.py b/numpy/random/_examples/numba/extending.py
index 0d240596b..f387db695 100644
--- a/numpy/random/_examples/numba/extending.py
+++ b/numpy/random/_examples/numba/extending.py
@@ -44,9 +44,9 @@ assert r1.shape == (n,)
assert r1.shape == r2.shape
t1 = timeit(numbacall, number=1000)
-print('{:.2f} secs for {} PCG64 (Numba/PCG64) gaussian randoms'.format(t1, n))
+print(f'{t1:.2f} secs for {n} PCG64 (Numba/PCG64) gaussian randoms')
t2 = timeit(numpycall, number=1000)
-print('{:.2f} secs for {} PCG64 (NumPy/PCG64) gaussian randoms'.format(t2, n))
+print(f'{t2:.2f} secs for {n} PCG64 (NumPy/PCG64) gaussian randoms')
# example 2
diff --git a/numpy/random/_generator.pyx b/numpy/random/_generator.pyx
index cc2852da7..7ffa36775 100644
--- a/numpy/random/_generator.pyx
+++ b/numpy/random/_generator.pyx
@@ -5,6 +5,7 @@ import warnings
from cpython.pycapsule cimport PyCapsule_IsValid, PyCapsule_GetPointer
from cpython cimport (Py_INCREF, PyFloat_AsDouble)
+from cpython.mem cimport PyMem_Malloc, PyMem_Free
cimport cython
import numpy as np
@@ -25,8 +26,16 @@ from ._common cimport (POISSON_LAM_MAX, CONS_POSITIVE, CONS_NONE,
CONS_GT_1, CONS_POSITIVE_NOT_NAN, CONS_POISSON,
double_fill, cont, kahan_sum, cont_broadcast_3, float_fill, cont_f,
check_array_constraint, check_constraint, disc, discrete_broadcast_iii,
+ validate_output_shape
)
+cdef extern from "numpy/arrayobject.h":
+ int PyArray_ResolveWritebackIfCopy(np.ndarray)
+ object PyArray_FromArray(np.PyArrayObject *, np.PyArray_Descr *, int)
+
+ enum:
+ NPY_ARRAY_WRITEBACKIFCOPY
+
np.import_array()
cdef int64_t _safe_sum_nonneg_int64(size_t num_colors, int64_t *colors):
@@ -47,6 +56,77 @@ cdef int64_t _safe_sum_nonneg_int64(size_t num_colors, int64_t *colors):
return sum
+cdef inline void _shuffle_raw_wrap(bitgen_t *bitgen, np.npy_intp n,
+ np.npy_intp first, np.npy_intp itemsize,
+ np.npy_intp stride,
+ char* data, char* buf) nogil:
+ # We trick gcc into providing a specialized implementation for
+ # the most common case, yielding a ~33% performance improvement.
+ # Note that apparently, only one branch can ever be specialized.
+ if itemsize == sizeof(np.npy_intp):
+ _shuffle_raw(bitgen, n, first, sizeof(np.npy_intp), stride, data, buf)
+ else:
+ _shuffle_raw(bitgen, n, first, itemsize, stride, data, buf)
+
+
+cdef inline void _shuffle_raw(bitgen_t *bitgen, np.npy_intp n,
+ np.npy_intp first, np.npy_intp itemsize,
+ np.npy_intp stride,
+ char* data, char* buf) nogil:
+ """
+ Parameters
+ ----------
+ bitgen
+ Pointer to a bitgen_t instance.
+ n
+ Number of elements in data
+ first
+ First observation to shuffle. Shuffles n-1,
+ n-2, ..., first, so that when first=1 the entire
+ array is shuffled
+ itemsize
+ Size in bytes of item
+ stride
+ Array stride
+ data
+ Location of data
+ buf
+ Location of buffer (itemsize)
+ """
+ cdef np.npy_intp i, j
+
+ for i in reversed(range(first, n)):
+ j = random_interval(bitgen, i)
+ string.memcpy(buf, data + j * stride, itemsize)
+ string.memcpy(data + j * stride, data + i * stride, itemsize)
+ string.memcpy(data + i * stride, buf, itemsize)
+
+
+cdef inline void _shuffle_int(bitgen_t *bitgen, np.npy_intp n,
+ np.npy_intp first, int64_t* data) nogil:
+ """
+ Parameters
+ ----------
+ bitgen
+ Pointer to a bitgen_t instance.
+ n
+ Number of elements in data
+ first
+ First observation to shuffle. Shuffles n-1,
+ n-2, ..., first, so that when first=1 the entire
+ array is shuffled
+ data
+ Location of data
+ """
+ cdef np.npy_intp i, j
+ cdef int64_t temp
+ for i in reversed(range(first, n)):
+ j = random_bounded_uint64(bitgen, 0, i, 0, 0)
+ temp = data[j]
+ data[j] = data[i]
+ data[i] = temp
+
+
cdef bint _check_bit_generator(object bitgen):
"""Check if an object satisfies the BitGenerator interface.
"""
@@ -707,8 +787,8 @@ cdef class Generator:
idx = np.PyArray_Arange(0, pop_size_i, 1, np.NPY_INT64)
idx_data = <int64_t*>(<np.ndarray>idx).data
with self.lock, nogil:
- self._shuffle_int(pop_size_i, max(pop_size_i - size_i, 1),
- idx_data)
+ _shuffle_int(&self._bitgen, pop_size_i,
+ max(pop_size_i - size_i, 1), idx_data)
# Copy to allow potentially large array backing idx to be gc
idx = idx[(pop_size - size):].copy()
else:
@@ -736,7 +816,7 @@ cdef class Generator:
hash_set[loc] = j
idx_data[j - pop_size_i + size_i] = j
if shuffle:
- self._shuffle_int(size_i, 1, idx_data)
+ _shuffle_int(&self._bitgen, size_i, 1, idx_data)
idx.shape = shape
if is_scalar and isinstance(idx, np.ndarray):
@@ -2809,6 +2889,7 @@ cdef class Generator:
cnt = np.PyArray_SIZE(randoms)
it = np.PyArray_MultiIterNew3(randoms, p_arr, n_arr)
+ validate_output_shape(it.shape, randoms)
with self.lock, nogil:
for i in range(cnt):
_dp = (<double*>np.PyArray_MultiIter_DATA(it, 1))[0]
@@ -3606,7 +3687,7 @@ cdef class Generator:
Now, do one experiment throwing the dice 10 time, and 10 times again,
and another throwing the dice 20 times, and 20 times again:
- >>> rng.multinomial([[10], [20]], [1/6.]*6, size=2)
+ >>> rng.multinomial([[10], [20]], [1/6.]*6, size=(2, 2))
array([[[2, 4, 0, 1, 2, 1],
[1, 3, 0, 3, 1, 2]],
[[1, 4, 4, 4, 4, 3],
@@ -3661,6 +3742,7 @@ cdef class Generator:
temp = np.empty(size, dtype=np.int8)
temp_arr = <np.ndarray>temp
it = np.PyArray_MultiIterNew2(on, temp_arr)
+ validate_output_shape(it.shape, temp_arr)
shape = it.shape + (d,)
multin = np.zeros(shape, dtype=np.int64)
mnarr = <np.ndarray>multin
@@ -3941,7 +4023,7 @@ cdef class Generator:
The drawn samples, of shape ``(size, k)``.
Raises
- -------
+ ------
ValueError
If any value in ``alpha`` is less than or equal to zero
@@ -4111,7 +4193,159 @@ cdef class Generator:
return diric
- # Shuffling and permutations:
+ def permuted(self, object x, *, axis=None, out=None):
+ """
+ permuted(x, axis=None, out=None)
+
+ Randomly permute `x` along axis `axis`.
+
+ Unlike `shuffle`, each slice along the given axis is shuffled
+ independently of the others.
+
+ Parameters
+ ----------
+ x : array_like, at least one-dimensional
+ Array to be shuffled.
+ axis : int, optional
+ Slices of `x` in this axis are shuffled. Each slice
+ is shuffled independently of the others. If `axis` is
+ None, the flattened array is shuffled.
+ out : ndarray, optional
+ If given, this is the destinaton of the shuffled array.
+ If `out` is None, a shuffled copy of the array is returned.
+
+ Returns
+ -------
+ ndarray
+ If `out` is None, a shuffled copy of `x` is returned.
+ Otherwise, the shuffled array is stored in `out`,
+ and `out` is returned
+
+ See Also
+ --------
+ shuffle
+ permutation
+
+ Examples
+ --------
+ Create a `numpy.random.Generator` instance:
+
+ >>> rng = np.random.default_rng()
+
+ Create a test array:
+
+ >>> x = np.arange(24).reshape(3, 8)
+ >>> x
+ array([[ 0, 1, 2, 3, 4, 5, 6, 7],
+ [ 8, 9, 10, 11, 12, 13, 14, 15],
+ [16, 17, 18, 19, 20, 21, 22, 23]])
+
+ Shuffle the rows of `x`:
+
+ >>> y = rng.permuted(x, axis=1)
+ >>> y
+ array([[ 4, 3, 6, 7, 1, 2, 5, 0], # random
+ [15, 10, 14, 9, 12, 11, 8, 13],
+ [17, 16, 20, 21, 18, 22, 23, 19]])
+
+ `x` has not been modified:
+
+ >>> x
+ array([[ 0, 1, 2, 3, 4, 5, 6, 7],
+ [ 8, 9, 10, 11, 12, 13, 14, 15],
+ [16, 17, 18, 19, 20, 21, 22, 23]])
+
+ To shuffle the rows of `x` in-place, pass `x` as the `out`
+ parameter:
+
+ >>> y = rng.permuted(x, axis=1, out=x)
+ >>> x
+ array([[ 3, 0, 4, 7, 1, 6, 2, 5], # random
+ [ 8, 14, 13, 9, 12, 11, 15, 10],
+ [17, 18, 16, 22, 19, 23, 20, 21]])
+
+ Note that when the ``out`` parameter is given, the return
+ value is ``out``:
+
+ >>> y is x
+ True
+ """
+
+ cdef int ax
+ cdef np.npy_intp axlen, axstride, itemsize
+ cdef void *buf
+ cdef np.flatiter it
+ cdef np.ndarray to_shuffle
+ cdef int status
+ cdef int flags
+
+ x = np.asarray(x)
+
+ if out is None:
+ out = x.copy(order='K')
+ else:
+ if type(out) is not np.ndarray:
+ raise TypeError('out must be a numpy array')
+ if out.shape != x.shape:
+ raise ValueError('out must have the same shape as x')
+ np.copyto(out, x, casting='safe')
+
+ if axis is None:
+ if x.ndim > 1:
+ if not (np.PyArray_FLAGS(out) & (np.NPY_ARRAY_C_CONTIGUOUS |
+ np.NPY_ARRAY_F_CONTIGUOUS)):
+ flags = (np.NPY_ARRAY_C_CONTIGUOUS |
+ NPY_ARRAY_WRITEBACKIFCOPY)
+ to_shuffle = PyArray_FromArray(<np.PyArrayObject *>out,
+ <np.PyArray_Descr *>NULL, flags)
+ self.shuffle(to_shuffle.ravel(order='K'))
+ # Because we only execute this block if out is not
+ # contiguous, we know this call will always result in a
+ # copy of to_shuffle back to out. I.e. status will be 1.
+ status = PyArray_ResolveWritebackIfCopy(to_shuffle)
+ assert status == 1
+ else:
+ # out is n-d with n > 1, but is either C- or F-contiguous,
+ # so we know out.ravel(order='A') is a view.
+ self.shuffle(out.ravel(order='A'))
+ else:
+ # out is 1-d
+ self.shuffle(out)
+ return out
+
+ ax = normalize_axis_index(axis, np.ndim(out))
+ itemsize = out.itemsize
+ axlen = out.shape[ax]
+ axstride = out.strides[ax]
+
+ it = np.PyArray_IterAllButAxis(out, &ax)
+
+ buf = PyMem_Malloc(itemsize)
+ if buf == NULL:
+ raise MemoryError('memory allocation failed in permuted')
+
+ if out.dtype.hasobject:
+ # Keep the GIL when shuffling an object array.
+ with self.lock:
+ while np.PyArray_ITER_NOTDONE(it):
+ _shuffle_raw_wrap(&self._bitgen, axlen, 0, itemsize,
+ axstride,
+ <char *>np.PyArray_ITER_DATA(it),
+ <char *>buf)
+ np.PyArray_ITER_NEXT(it)
+ else:
+ # out is not an object array, so we can release the GIL.
+ with self.lock, nogil:
+ while np.PyArray_ITER_NOTDONE(it):
+ _shuffle_raw_wrap(&self._bitgen, axlen, 0, itemsize,
+ axstride,
+ <char *>np.PyArray_ITER_DATA(it),
+ <char *>buf)
+ np.PyArray_ITER_NEXT(it)
+
+ PyMem_Free(buf)
+ return out
+
def shuffle(self, object x, axis=0):
"""
shuffle(x, axis=0)
@@ -4174,14 +4408,15 @@ cdef class Generator:
# when the function exits.
buf = np.empty(itemsize, dtype=np.int8) # GC'd at function exit
buf_ptr = <char*><size_t>np.PyArray_DATA(buf)
- with self.lock:
- # We trick gcc into providing a specialized implementation for
- # the most common case, yielding a ~33% performance improvement.
- # Note that apparently, only one branch can ever be specialized.
- if itemsize == sizeof(np.npy_intp):
- self._shuffle_raw(n, 1, sizeof(np.npy_intp), stride, x_ptr, buf_ptr)
- else:
- self._shuffle_raw(n, 1, itemsize, stride, x_ptr, buf_ptr)
+ if x.dtype.hasobject:
+ with self.lock:
+ _shuffle_raw_wrap(&self._bitgen, n, 1, itemsize, stride,
+ x_ptr, buf_ptr)
+ else:
+ # Same as above, but the GIL is released.
+ with self.lock, nogil:
+ _shuffle_raw_wrap(&self._bitgen, n, 1, itemsize, stride,
+ x_ptr, buf_ptr)
elif isinstance(x, np.ndarray) and x.ndim and x.size:
x = np.swapaxes(x, 0, axis)
buf = np.empty_like(x[0, ...])
@@ -4204,56 +4439,6 @@ cdef class Generator:
j = random_interval(&self._bitgen, i)
x[i], x[j] = x[j], x[i]
- cdef inline _shuffle_raw(self, np.npy_intp n, np.npy_intp first,
- np.npy_intp itemsize, np.npy_intp stride,
- char* data, char* buf):
- """
- Parameters
- ----------
- n
- Number of elements in data
- first
- First observation to shuffle. Shuffles n-1,
- n-2, ..., first, so that when first=1 the entire
- array is shuffled
- itemsize
- Size in bytes of item
- stride
- Array stride
- data
- Location of data
- buf
- Location of buffer (itemsize)
- """
- cdef np.npy_intp i, j
- for i in reversed(range(first, n)):
- j = random_interval(&self._bitgen, i)
- string.memcpy(buf, data + j * stride, itemsize)
- string.memcpy(data + j * stride, data + i * stride, itemsize)
- string.memcpy(data + i * stride, buf, itemsize)
-
- cdef inline void _shuffle_int(self, np.npy_intp n, np.npy_intp first,
- int64_t* data) nogil:
- """
- Parameters
- ----------
- n
- Number of elements in data
- first
- First observation to shuffle. Shuffles n-1,
- n-2, ..., first, so that when first=1 the entire
- array is shuffled
- data
- Location of data
- """
- cdef np.npy_intp i, j
- cdef int64_t temp
- for i in reversed(range(first, n)):
- j = random_bounded_uint64(&self._bitgen, 0, i, 0, 0)
- temp = data[j]
- data[j] = data[i]
- data[i] = temp
-
def permutation(self, object x, axis=0):
"""
permutation(x, axis=0)
@@ -4336,7 +4521,7 @@ def default_rng(seed=None):
unpredictable entropy will be pulled from the OS. If an ``int`` or
``array_like[ints]`` is passed, then it will be passed to
`SeedSequence` to derive the initial `BitGenerator` state. One may also
- pass in a`SeedSequence` instance
+ pass in a `SeedSequence` instance.
Additionally, when passed a `BitGenerator`, it will be wrapped by
`Generator`. If passed a `Generator`, it will be returned unaltered.
diff --git a/numpy/random/bit_generator.pxd b/numpy/random/bit_generator.pxd
index bd5e47a20..dfa7d0a71 100644
--- a/numpy/random/bit_generator.pxd
+++ b/numpy/random/bit_generator.pxd
@@ -23,7 +23,7 @@ cdef class BitGenerator():
cdef class SeedSequence():
cdef readonly object entropy
cdef readonly tuple spawn_key
- cdef readonly uint32_t pool_size
+ cdef readonly Py_ssize_t pool_size
cdef readonly object pool
cdef readonly uint32_t n_children_spawned
diff --git a/numpy/random/mtrand.pyx b/numpy/random/mtrand.pyx
index 8820a6e09..d43e7f5aa 100644
--- a/numpy/random/mtrand.pyx
+++ b/numpy/random/mtrand.pyx
@@ -22,6 +22,7 @@ from ._common cimport (POISSON_LAM_MAX, CONS_POSITIVE, CONS_NONE,
CONS_GT_1, LEGACY_CONS_POISSON,
double_fill, cont, kahan_sum, cont_broadcast_3,
check_array_constraint, check_constraint, disc, discrete_broadcast_iii,
+ validate_output_shape
)
cdef extern from "numpy/random/distributions.h":
@@ -382,7 +383,7 @@ cdef class RandomState:
.. note::
New code should use the ``random`` method of a ``default_rng()``
- instance instead; see `random-quick-start`.
+ instance instead; please see the :ref:`random-quick-start`.
Parameters
----------
@@ -452,7 +453,7 @@ cdef class RandomState:
.. note::
New code should use the ``beta`` method of a ``default_rng()``
- instance instead; see `random-quick-start`.
+ instance instead; please see the :ref:`random-quick-start`.
Parameters
----------
@@ -502,7 +503,7 @@ cdef class RandomState:
.. note::
New code should use the ``exponential`` method of a ``default_rng()``
- instance instead; see `random-quick-start`.
+ instance instead; please see the :ref:`random-quick-start`.
Parameters
----------
@@ -551,7 +552,7 @@ cdef class RandomState:
.. note::
New code should use the ``standard_exponential`` method of a ``default_rng()``
- instance instead; see `random-quick-start`.
+ instance instead; please see the :ref:`random-quick-start`.
Parameters
----------
@@ -652,7 +653,7 @@ cdef class RandomState:
.. note::
New code should use the ``integers`` method of a ``default_rng()``
- instance instead; see `random-quick-start`.
+ instance instead; please see the :ref:`random-quick-start`.
Parameters
----------
@@ -773,7 +774,7 @@ cdef class RandomState:
.. note::
New code should use the ``bytes`` method of a ``default_rng()``
- instance instead; see `random-quick-start`.
+ instance instead; please see the :ref:`random-quick-start`.
Parameters
----------
@@ -811,7 +812,7 @@ cdef class RandomState:
.. note::
New code should use the ``choice`` method of a ``default_rng()``
- instance instead; see `random-quick-start`.
+ instance instead; please see the :ref:`random-quick-start`.
Parameters
----------
@@ -1016,7 +1017,7 @@ cdef class RandomState:
.. note::
New code should use the ``uniform`` method of a ``default_rng()``
- instance instead; see `random-quick-start`.
+ instance instead; please see the :ref:`random-quick-start`.
Parameters
----------
@@ -1184,7 +1185,7 @@ cdef class RandomState:
.. note::
New code should use the ``standard_normal`` method of a ``default_rng()``
- instance instead; see `random-quick-start`.
+ instance instead; please see the :ref:`random-quick-start`.
If positive int_like arguments are provided, `randn` generates an array
of shape ``(d0, d1, ..., dn)``, filled
@@ -1338,7 +1339,7 @@ cdef class RandomState:
.. note::
New code should use the ``standard_normal`` method of a ``default_rng()``
- instance instead; see `random-quick-start`.
+ instance instead; please see the :ref:`random-quick-start`.
Parameters
----------
@@ -1413,7 +1414,7 @@ cdef class RandomState:
.. note::
New code should use the ``normal`` method of a ``default_rng()``
- instance instead; see `random-quick-start`.
+ instance instead; please see the :ref:`random-quick-start`.
Parameters
----------
@@ -1513,7 +1514,7 @@ cdef class RandomState:
.. note::
New code should use the ``standard_gamma`` method of a ``default_rng()``
- instance instead; see `random-quick-start`.
+ instance instead; please see the :ref:`random-quick-start`.
Parameters
----------
@@ -1594,7 +1595,7 @@ cdef class RandomState:
.. note::
New code should use the ``gamma`` method of a ``default_rng()``
- instance instead; see `random-quick-start`.
+ instance instead; please see the :ref:`random-quick-start`.
Parameters
----------
@@ -1683,7 +1684,7 @@ cdef class RandomState:
.. note::
New code should use the ``f`` method of a ``default_rng()``
- instance instead; see `random-quick-start`.
+ instance instead; please see the :ref:`random-quick-start`.
Parameters
----------
@@ -1771,7 +1772,7 @@ cdef class RandomState:
.. note::
New code should use the ``noncentral_f`` method of a ``default_rng()``
- instance instead; see `random-quick-start`.
+ instance instead; please see the :ref:`random-quick-start`.
Parameters
----------
@@ -1856,7 +1857,7 @@ cdef class RandomState:
.. note::
New code should use the ``chisquare`` method of a ``default_rng()``
- instance instead; see `random-quick-start`.
+ instance instead; please see the :ref:`random-quick-start`.
Parameters
----------
@@ -1929,7 +1930,7 @@ cdef class RandomState:
.. note::
New code should use the ``noncentral_chisquare`` method of a ``default_rng()``
- instance instead; see `random-quick-start`.
+ instance instead; please see the :ref:`random-quick-start`.
Parameters
----------
@@ -2015,7 +2016,7 @@ cdef class RandomState:
.. note::
New code should use the ``standard_cauchy`` method of a ``default_rng()``
- instance instead; see `random-quick-start`.
+ instance instead; please see the :ref:`random-quick-start`.
Parameters
----------
@@ -2091,7 +2092,7 @@ cdef class RandomState:
.. note::
New code should use the ``standard_t`` method of a ``default_rng()``
- instance instead; see `random-quick-start`.
+ instance instead; please see the :ref:`random-quick-start`.
Parameters
----------
@@ -2196,7 +2197,7 @@ cdef class RandomState:
.. note::
New code should use the ``vonmises`` method of a ``default_rng()``
- instance instead; see `random-quick-start`.
+ instance instead; please see the :ref:`random-quick-start`.
Parameters
----------
@@ -2294,7 +2295,7 @@ cdef class RandomState:
.. note::
New code should use the ``pareto`` method of a ``default_rng()``
- instance instead; see `random-quick-start`.
+ instance instead; please see the :ref:`random-quick-start`.
Parameters
----------
@@ -2388,7 +2389,7 @@ cdef class RandomState:
.. note::
New code should use the ``weibull`` method of a ``default_rng()``
- instance instead; see `random-quick-start`.
+ instance instead; please see the :ref:`random-quick-start`.
Parameters
----------
@@ -2484,7 +2485,7 @@ cdef class RandomState:
.. note::
New code should use the ``power`` method of a ``default_rng()``
- instance instead; see `random-quick-start`.
+ instance instead; please see the :ref:`random-quick-start`.
Parameters
----------
@@ -2595,7 +2596,7 @@ cdef class RandomState:
.. note::
New code should use the ``laplace`` method of a ``default_rng()``
- instance instead; see `random-quick-start`.
+ instance instead; please see the :ref:`random-quick-start`.
Parameters
----------
@@ -2686,7 +2687,7 @@ cdef class RandomState:
.. note::
New code should use the ``gumbel`` method of a ``default_rng()``
- instance instead; see `random-quick-start`.
+ instance instead; please see the :ref:`random-quick-start`.
Parameters
----------
@@ -2808,7 +2809,7 @@ cdef class RandomState:
.. note::
New code should use the ``logistic`` method of a ``default_rng()``
- instance instead; see `random-quick-start`.
+ instance instead; please see the :ref:`random-quick-start`.
Parameters
----------
@@ -2895,7 +2896,7 @@ cdef class RandomState:
.. note::
New code should use the ``lognormal`` method of a ``default_rng()``
- instance instead; see `random-quick-start`.
+ instance instead; please see the :ref:`random-quick-start`.
Parameters
----------
@@ -3008,7 +3009,7 @@ cdef class RandomState:
.. note::
New code should use the ``rayleigh`` method of a ``default_rng()``
- instance instead; see `random-quick-start`.
+ instance instead; please see the :ref:`random-quick-start`.
Parameters
----------
@@ -3090,7 +3091,7 @@ cdef class RandomState:
.. note::
New code should use the ``wald`` method of a ``default_rng()``
- instance instead; see `random-quick-start`.
+ instance instead; please see the :ref:`random-quick-start`.
Parameters
----------
@@ -3163,7 +3164,7 @@ cdef class RandomState:
.. note::
New code should use the ``triangular`` method of a ``default_rng()``
- instance instead; see `random-quick-start`.
+ instance instead; please see the :ref:`random-quick-start`.
Parameters
----------
@@ -3270,7 +3271,7 @@ cdef class RandomState:
.. note::
New code should use the ``binomial`` method of a ``default_rng()``
- instance instead; see `random-quick-start`.
+ instance instead; please see the :ref:`random-quick-start`.
Parameters
----------
@@ -3374,6 +3375,7 @@ cdef class RandomState:
cnt = np.PyArray_SIZE(randoms)
it = np.PyArray_MultiIterNew3(randoms, p_arr, n_arr)
+ validate_output_shape(it.shape, randoms)
with self.lock, nogil:
for i in range(cnt):
_dp = (<double*>np.PyArray_MultiIter_DATA(it, 1))[0]
@@ -3419,7 +3421,7 @@ cdef class RandomState:
.. note::
New code should use the ``negative_binomial`` method of a ``default_rng()``
- instance instead; see `random-quick-start`.
+ instance instead; please see the :ref:`random-quick-start`.
Parameters
----------
@@ -3504,7 +3506,7 @@ cdef class RandomState:
.. note::
New code should use the ``poisson`` method of a ``default_rng()``
- instance instead; see `random-quick-start`.
+ instance instead; please see the :ref:`random-quick-start`.
Parameters
----------
@@ -3590,7 +3592,7 @@ cdef class RandomState:
.. note::
New code should use the ``zipf`` method of a ``default_rng()``
- instance instead; see `random-quick-start`.
+ instance instead; please see the :ref:`random-quick-start`.
Parameters
----------
@@ -3680,7 +3682,7 @@ cdef class RandomState:
.. note::
New code should use the ``geometric`` method of a ``default_rng()``
- instance instead; see `random-quick-start`.
+ instance instead; please see the :ref:`random-quick-start`.
Parameters
----------
@@ -3734,7 +3736,7 @@ cdef class RandomState:
.. note::
New code should use the ``hypergeometric`` method of a ``default_rng()``
- instance instead; see `random-quick-start`.
+ instance instead; please see the :ref:`random-quick-start`.
Parameters
----------
@@ -3865,7 +3867,7 @@ cdef class RandomState:
.. note::
New code should use the ``logseries`` method of a ``default_rng()``
- instance instead; see `random-quick-start`.
+ instance instead; please see the :ref:`random-quick-start`.
Parameters
----------
@@ -3958,7 +3960,7 @@ cdef class RandomState:
.. note::
New code should use the ``multivariate_normal`` method of a ``default_rng()``
- instance instead; see `random-quick-start`.
+ instance instead; please see the :ref:`random-quick-start`.
Parameters
----------
@@ -4132,7 +4134,7 @@ cdef class RandomState:
.. note::
New code should use the ``multinomial`` method of a ``default_rng()``
- instance instead; see `random-quick-start`.
+ instance instead; please see the :ref:`random-quick-start`.
Parameters
----------
@@ -4250,7 +4252,7 @@ cdef class RandomState:
.. note::
New code should use the ``dirichlet`` method of a ``default_rng()``
- instance instead; see `random-quick-start`.
+ instance instead; please see the :ref:`random-quick-start`.
Parameters
----------
@@ -4396,7 +4398,7 @@ cdef class RandomState:
.. note::
New code should use the ``shuffle`` method of a ``default_rng()``
- instance instead; see `random-quick-start`.
+ instance instead; please see the :ref:`random-quick-start`.
Parameters
----------
@@ -4491,7 +4493,7 @@ cdef class RandomState:
.. note::
New code should use the ``permutation`` method of a ``default_rng()``
- instance instead; see `random-quick-start`.
+ instance instead; please see the :ref:`random-quick-start`.
Parameters
----------
diff --git a/numpy/random/setup.py b/numpy/random/setup.py
index 88ddb1268..bfd08e469 100644
--- a/numpy/random/setup.py
+++ b/numpy/random/setup.py
@@ -137,6 +137,7 @@ def configuration(parent_package='', top_path=None):
define_macros=defs + LEGACY_DEFS,
)
config.add_data_files(*depends)
+ config.add_data_files('*.pyi')
return config
diff --git a/numpy/random/src/pcg64/pcg64.h b/numpy/random/src/pcg64/pcg64.h
index 2a7217dd9..31899cbc1 100644
--- a/numpy/random/src/pcg64/pcg64.h
+++ b/numpy/random/src/pcg64/pcg64.h
@@ -57,11 +57,11 @@
#define inline __forceinline
#endif
-#if __GNUC_GNU_INLINE__ && !defined(__cplusplus)
+#if defined(__GNUC_GNU_INLINE__) && !defined(__cplusplus)
#error Nonstandard GNU inlining semantics. Compile with -std=c99 or better.
#endif
-#if __cplusplus
+#ifdef __cplusplus
extern "C" {
#endif
@@ -255,7 +255,7 @@ typedef pcg_state_setseq_128 pcg64_random_t;
#define pcg64_advance_r pcg_setseq_128_advance_r
#define PCG64_INITIALIZER PCG_STATE_SETSEQ_128_INITIALIZER
-#if __cplusplus
+#ifdef __cplusplus
}
#endif
diff --git a/numpy/random/tests/test_direct.py b/numpy/random/tests/test_direct.py
index dad12c8a8..d602b36b4 100644
--- a/numpy/random/tests/test_direct.py
+++ b/numpy/random/tests/test_direct.py
@@ -230,13 +230,13 @@ class Base:
def test_repr(self):
rs = Generator(self.bit_generator(*self.data1['seed']))
assert 'Generator' in repr(rs)
- assert '{:#x}'.format(id(rs)).upper().replace('X', 'x') in repr(rs)
+ assert f'{id(rs):#x}'.upper().replace('X', 'x') in repr(rs)
def test_str(self):
rs = Generator(self.bit_generator(*self.data1['seed']))
assert 'Generator' in str(rs)
assert str(self.bit_generator.__name__) in str(rs)
- assert '{:#x}'.format(id(rs)).upper().replace('X', 'x') not in str(rs)
+ assert f'{id(rs):#x}'.upper().replace('X', 'x') not in str(rs)
def test_pickle(self):
import pickle
diff --git a/numpy/random/tests/test_generator_mt19937.py b/numpy/random/tests/test_generator_mt19937.py
index 332b63198..b69cd38d4 100644
--- a/numpy/random/tests/test_generator_mt19937.py
+++ b/numpy/random/tests/test_generator_mt19937.py
@@ -18,20 +18,20 @@ JUMP_TEST_DATA = [
{
"seed": 0,
"steps": 10,
- "initial": {"key_md5": "64eaf265d2203179fb5ffb73380cd589", "pos": 9},
- "jumped": {"key_md5": "8cb7b061136efceef5217a9ce2cc9a5a", "pos": 598},
+ "initial": {"key_sha256": "bb1636883c2707b51c5b7fc26c6927af4430f2e0785a8c7bc886337f919f9edf", "pos": 9},
+ "jumped": {"key_sha256": "ff682ac12bb140f2d72fba8d3506cf4e46817a0db27aae1683867629031d8d55", "pos": 598},
},
{
"seed":384908324,
"steps":312,
- "initial": {"key_md5": "e99708a47b82ff51a2c7b0625b81afb5", "pos": 311},
- "jumped": {"key_md5": "2ecdbfc47a895b253e6e19ccb2e74b90", "pos": 276},
+ "initial": {"key_sha256": "16b791a1e04886ccbbb4d448d6ff791267dc458ae599475d08d5cced29d11614", "pos": 311},
+ "jumped": {"key_sha256": "a0110a2cf23b56be0feaed8f787a7fc84bef0cb5623003d75b26bdfa1c18002c", "pos": 276},
},
{
"seed": [839438204, 980239840, 859048019, 821],
"steps": 511,
- "initial": {"key_md5": "9fcd6280df9199785e17e93162ce283c", "pos": 510},
- "jumped": {"key_md5": "433b85229f2ed853cde06cd872818305", "pos": 475},
+ "initial": {"key_sha256": "d306cf01314d51bd37892d874308200951a35265ede54d200f1e065004c3e9ea", "pos": 510},
+ "jumped": {"key_sha256": "0e00ab449f01a5195a83b4aee0dfbc2ce8d46466a640b92e33977d2e42f777f8", "pos": 475},
},
]
@@ -483,18 +483,18 @@ class TestIntegers:
assert_array_equal(scalar, array)
def test_repeatability(self, endpoint):
- # We use a md5 hash of generated sequences of 1000 samples
+ # We use a sha256 hash of generated sequences of 1000 samples
# in the range [0, 6) for all but bool, where the range
# is [0, 2). Hashes are for little endian numbers.
- tgt = {'bool': 'b3300e66d2bb59e493d255d47c3a6cbe',
- 'int16': '39624ead49ad67e37545744024d2648b',
- 'int32': '5c4810373f979336c6c0c999996e47a1',
- 'int64': 'ab126c15edff26f55c50d2b7e37391ac',
- 'int8': 'ba71ccaffeeeb9eeb1860f8075020b9c',
- 'uint16': '39624ead49ad67e37545744024d2648b',
- 'uint32': '5c4810373f979336c6c0c999996e47a1',
- 'uint64': 'ab126c15edff26f55c50d2b7e37391ac',
- 'uint8': 'ba71ccaffeeeb9eeb1860f8075020b9c'}
+ tgt = {'bool': '053594a9b82d656f967c54869bc6970aa0358cf94ad469c81478459c6a90eee3',
+ 'int16': '54de9072b6ee9ff7f20b58329556a46a447a8a29d67db51201bf88baa6e4e5d4',
+ 'int32': 'd3a0d5efb04542b25ac712e50d21f39ac30f312a5052e9bbb1ad3baa791ac84b',
+ 'int64': '14e224389ac4580bfbdccb5697d6190b496f91227cf67df60989de3d546389b1',
+ 'int8': '0e203226ff3fbbd1580f15da4621e5f7164d0d8d6b51696dd42d004ece2cbec1',
+ 'uint16': '54de9072b6ee9ff7f20b58329556a46a447a8a29d67db51201bf88baa6e4e5d4',
+ 'uint32': 'd3a0d5efb04542b25ac712e50d21f39ac30f312a5052e9bbb1ad3baa791ac84b',
+ 'uint64': '14e224389ac4580bfbdccb5697d6190b496f91227cf67df60989de3d546389b1',
+ 'uint8': '0e203226ff3fbbd1580f15da4621e5f7164d0d8d6b51696dd42d004ece2cbec1'}
for dt in self.itype[1:]:
random = Generator(MT19937(1234))
@@ -507,14 +507,14 @@ class TestIntegers:
val = random.integers(0, 6 - endpoint, size=1000, endpoint=endpoint,
dtype=dt).byteswap()
- res = hashlib.md5(val).hexdigest()
+ res = hashlib.sha256(val).hexdigest()
assert_(tgt[np.dtype(dt).name] == res)
# bools do not depend on endianness
random = Generator(MT19937(1234))
val = random.integers(0, 2 - endpoint, size=1000, endpoint=endpoint,
dtype=bool).view(np.int8)
- res = hashlib.md5(val).hexdigest()
+ res = hashlib.sha256(val).hexdigest()
assert_(tgt[np.dtype(bool).name] == res)
def test_repeatability_broadcasting(self, endpoint):
@@ -905,12 +905,12 @@ class TestRandomDist:
assert actual.dtype == np.int64
def test_choice_large_sample(self):
- choice_hash = 'd44962a0b1e92f4a3373c23222244e21'
+ choice_hash = '4266599d12bfcfb815213303432341c06b4349f5455890446578877bb322e222'
random = Generator(MT19937(self.seed))
actual = random.choice(10000, 5000, replace=False)
if sys.byteorder != 'little':
actual = actual.byteswap()
- res = hashlib.md5(actual.view(np.int8)).hexdigest()
+ res = hashlib.sha256(actual.view(np.int8)).hexdigest()
assert_(choice_hash == res)
def test_bytes(self):
@@ -1039,6 +1039,56 @@ class TestRandomDist:
assert_raises(np.AxisError, random.permutation, arr, 3)
assert_raises(TypeError, random.permutation, arr, slice(1, 2, None))
+ @pytest.mark.parametrize("dtype", [int, object])
+ @pytest.mark.parametrize("axis, expected",
+ [(None, np.array([[3, 7, 0, 9, 10, 11],
+ [8, 4, 2, 5, 1, 6]])),
+ (0, np.array([[6, 1, 2, 9, 10, 11],
+ [0, 7, 8, 3, 4, 5]])),
+ (1, np.array([[ 5, 3, 4, 0, 2, 1],
+ [11, 9, 10, 6, 8, 7]]))])
+ def test_permuted(self, dtype, axis, expected):
+ random = Generator(MT19937(self.seed))
+ x = np.arange(12).reshape(2, 6).astype(dtype)
+ random.permuted(x, axis=axis, out=x)
+ assert_array_equal(x, expected)
+
+ random = Generator(MT19937(self.seed))
+ x = np.arange(12).reshape(2, 6).astype(dtype)
+ y = random.permuted(x, axis=axis)
+ assert y.dtype == dtype
+ assert_array_equal(y, expected)
+
+ def test_permuted_with_strides(self):
+ random = Generator(MT19937(self.seed))
+ x0 = np.arange(22).reshape(2, 11)
+ x1 = x0.copy()
+ x = x0[:, ::3]
+ y = random.permuted(x, axis=1, out=x)
+ expected = np.array([[0, 9, 3, 6],
+ [14, 20, 11, 17]])
+ assert_array_equal(y, expected)
+ x1[:, ::3] = expected
+ # Verify that the original x0 was modified in-place as expected.
+ assert_array_equal(x1, x0)
+
+ def test_permuted_empty(self):
+ y = random.permuted([])
+ assert_array_equal(y, [])
+
+ @pytest.mark.parametrize('outshape', [(2, 3), 5])
+ def test_permuted_out_with_wrong_shape(self, outshape):
+ a = np.array([1, 2, 3])
+ out = np.zeros(outshape, dtype=a.dtype)
+ with pytest.raises(ValueError, match='same shape'):
+ random.permuted(a, out=out)
+
+ def test_permuted_out_with_wrong_type(self):
+ out = np.zeros((3, 5), dtype=np.int32)
+ x = np.ones((3, 5))
+ with pytest.raises(TypeError, match='Cannot cast'):
+ random.permuted(x, axis=1, out=out)
+
def test_beta(self):
random = Generator(MT19937(self.seed))
actual = random.beta(.1, .9, size=(3, 2))
@@ -2374,7 +2424,7 @@ class TestSingleEltArrayInput:
@pytest.mark.parametrize("config", JUMP_TEST_DATA)
def test_jumped(config):
# Each config contains the initial seed, a number of raw steps
- # the md5 hashes of the initial and the final states' keys and
+ # the sha256 hashes of the initial and the final states' keys and
# the position of of the initial and the final state.
# These were produced using the original C implementation.
seed = config["seed"]
@@ -2386,17 +2436,17 @@ def test_jumped(config):
key = mt19937.state["state"]["key"]
if sys.byteorder == 'big':
key = key.byteswap()
- md5 = hashlib.md5(key)
+ sha256 = hashlib.sha256(key)
assert mt19937.state["state"]["pos"] == config["initial"]["pos"]
- assert md5.hexdigest() == config["initial"]["key_md5"]
+ assert sha256.hexdigest() == config["initial"]["key_sha256"]
jumped = mt19937.jumped()
key = jumped.state["state"]["key"]
if sys.byteorder == 'big':
key = key.byteswap()
- md5 = hashlib.md5(key)
+ sha256 = hashlib.sha256(key)
assert jumped.state["state"]["pos"] == config["jumped"]["pos"]
- assert md5.hexdigest() == config["jumped"]["key_md5"]
+ assert sha256.hexdigest() == config["jumped"]["key_sha256"]
def test_broadcast_size_error():
@@ -2423,6 +2473,16 @@ def test_broadcast_size_error():
with pytest.raises(ValueError):
random.standard_gamma(shape, out=out)
+ # 2 arg
+ with pytest.raises(ValueError):
+ random.binomial(1, [0.3, 0.7], size=(2, 1))
+ with pytest.raises(ValueError):
+ random.binomial([1, 2], 0.3, size=(2, 1))
+ with pytest.raises(ValueError):
+ random.binomial([1, 2], [0.3, 0.7], size=(2, 1))
+ with pytest.raises(ValueError):
+ random.multinomial([2, 2], [.3, .7], size=(2, 1))
+
# 3 arg
a = random.chisquare(5, size=3)
b = random.chisquare(5, size=(4, 3))
diff --git a/numpy/random/tests/test_generator_mt19937_regressions.py b/numpy/random/tests/test_generator_mt19937_regressions.py
index 456c932d4..2ef6b0631 100644
--- a/numpy/random/tests/test_generator_mt19937_regressions.py
+++ b/numpy/random/tests/test_generator_mt19937_regressions.py
@@ -33,11 +33,11 @@ class TestRegression:
# numbers with this large sample
# theoretical large N result is 0.49706795
freq = np.sum(rvsn == 1) / float(N)
- msg = "Frequency was %f, should be > 0.45" % freq
+ msg = f'Frequency was {freq:f}, should be > 0.45'
assert_(freq > 0.45, msg)
# theoretical large N result is 0.19882718
freq = np.sum(rvsn == 2) / float(N)
- msg = "Frequency was %f, should be < 0.23" % freq
+ msg = f'Frequency was {freq:f}, should be < 0.23'
assert_(freq < 0.23, msg)
def test_shuffle_mixed_dimension(self):
diff --git a/numpy/random/tests/test_random.py b/numpy/random/tests/test_random.py
index 276b5bc81..c13fc39e3 100644
--- a/numpy/random/tests/test_random.py
+++ b/numpy/random/tests/test_random.py
@@ -211,18 +211,18 @@ class TestRandint:
def test_repeatability(self):
import hashlib
- # We use a md5 hash of generated sequences of 1000 samples
+ # We use a sha256 hash of generated sequences of 1000 samples
# in the range [0, 6) for all but bool, where the range
# is [0, 2). Hashes are for little endian numbers.
- tgt = {'bool': '7dd3170d7aa461d201a65f8bcf3944b0',
- 'int16': '1b7741b80964bb190c50d541dca1cac1',
- 'int32': '4dc9fcc2b395577ebb51793e58ed1a05',
- 'int64': '17db902806f448331b5a758d7d2ee672',
- 'int8': '27dd30c4e08a797063dffac2490b0be6',
- 'uint16': '1b7741b80964bb190c50d541dca1cac1',
- 'uint32': '4dc9fcc2b395577ebb51793e58ed1a05',
- 'uint64': '17db902806f448331b5a758d7d2ee672',
- 'uint8': '27dd30c4e08a797063dffac2490b0be6'}
+ tgt = {'bool': '509aea74d792fb931784c4b0135392c65aec64beee12b0cc167548a2c3d31e71',
+ 'int16': '7b07f1a920e46f6d0fe02314155a2330bcfd7635e708da50e536c5ebb631a7d4',
+ 'int32': 'e577bfed6c935de944424667e3da285012e741892dcb7051a8f1ce68ab05c92f',
+ 'int64': '0fbead0b06759df2cfb55e43148822d4a1ff953c7eb19a5b08445a63bb64fa9e',
+ 'int8': '001aac3a5acb935a9b186cbe14a1ca064b8bb2dd0b045d48abeacf74d0203404',
+ 'uint16': '7b07f1a920e46f6d0fe02314155a2330bcfd7635e708da50e536c5ebb631a7d4',
+ 'uint32': 'e577bfed6c935de944424667e3da285012e741892dcb7051a8f1ce68ab05c92f',
+ 'uint64': '0fbead0b06759df2cfb55e43148822d4a1ff953c7eb19a5b08445a63bb64fa9e',
+ 'uint8': '001aac3a5acb935a9b186cbe14a1ca064b8bb2dd0b045d48abeacf74d0203404'}
for dt in self.itype[1:]:
np.random.seed(1234)
@@ -233,13 +233,13 @@ class TestRandint:
else:
val = self.rfunc(0, 6, size=1000, dtype=dt).byteswap()
- res = hashlib.md5(val.view(np.int8)).hexdigest()
+ res = hashlib.sha256(val.view(np.int8)).hexdigest()
assert_(tgt[np.dtype(dt).name] == res)
# bools do not depend on endianness
np.random.seed(1234)
val = self.rfunc(0, 2, size=1000, dtype=bool).view(np.int8)
- res = hashlib.md5(val).hexdigest()
+ res = hashlib.sha256(val).hexdigest()
assert_(tgt[np.dtype(bool).name] == res)
def test_int64_uint64_corner_case(self):
diff --git a/numpy/random/tests/test_randomstate.py b/numpy/random/tests/test_randomstate.py
index edd7811bf..b70a04347 100644
--- a/numpy/random/tests/test_randomstate.py
+++ b/numpy/random/tests/test_randomstate.py
@@ -26,24 +26,24 @@ INT_FUNCS = {'binomial': (100.0, 0.6),
if np.iinfo(int).max < 2**32:
# Windows and some 32-bit platforms, e.g., ARM
- INT_FUNC_HASHES = {'binomial': '670e1c04223ffdbab27e08fbbad7bdba',
- 'logseries': '6bd0183d2f8030c61b0d6e11aaa60caf',
- 'geometric': '6e9df886f3e1e15a643168568d5280c0',
- 'hypergeometric': '7964aa611b046aecd33063b90f4dec06',
- 'multinomial': '68a0b049c16411ed0aa4aff3572431e4',
- 'negative_binomial': 'dc265219eec62b4338d39f849cd36d09',
- 'poisson': '7b4dce8e43552fc82701c2fa8e94dc6e',
- 'zipf': 'fcd2a2095f34578723ac45e43aca48c5',
+ INT_FUNC_HASHES = {'binomial': '2fbead005fc63942decb5326d36a1f32fe2c9d32c904ee61e46866b88447c263',
+ 'logseries': '23ead5dcde35d4cfd4ef2c105e4c3d43304b45dc1b1444b7823b9ee4fa144ebb',
+ 'geometric': '0d764db64f5c3bad48c8c33551c13b4d07a1e7b470f77629bef6c985cac76fcf',
+ 'hypergeometric': '7b59bf2f1691626c5815cdcd9a49e1dd68697251d4521575219e4d2a1b8b2c67',
+ 'multinomial': 'd754fa5b92943a38ec07630de92362dd2e02c43577fc147417dc5b9db94ccdd3',
+ 'negative_binomial': '8eb216f7cb2a63cf55605422845caaff002fddc64a7dc8b2d45acd477a49e824',
+ 'poisson': '70c891d76104013ebd6f6bcf30d403a9074b886ff62e4e6b8eb605bf1a4673b7',
+ 'zipf': '01f074f97517cd5d21747148ac6ca4074dde7fcb7acbaec0a936606fecacd93f',
}
else:
- INT_FUNC_HASHES = {'binomial': 'b5f8dcd74f172836536deb3547257b14',
- 'geometric': '8814571f45c87c59699d62ccd3d6c350',
- 'hypergeometric': 'bc64ae5976eac452115a16dad2dcf642',
- 'logseries': '84be924b37485a27c4a98797bc88a7a4',
- 'multinomial': 'ec3c7f9cf9664044bb0c6fb106934200',
- 'negative_binomial': '210533b2234943591364d0117a552969',
- 'poisson': '0536a8850c79da0c78defd742dccc3e0',
- 'zipf': 'f2841f504dd2525cd67cdcad7561e532',
+ INT_FUNC_HASHES = {'binomial': '8626dd9d052cb608e93d8868de0a7b347258b199493871a1dc56e2a26cacb112',
+ 'geometric': '8edd53d272e49c4fc8fbbe6c7d08d563d62e482921f3131d0a0e068af30f0db9',
+ 'hypergeometric': '83496cc4281c77b786c9b7ad88b74d42e01603a55c60577ebab81c3ba8d45657',
+ 'logseries': '65878a38747c176bc00e930ebafebb69d4e1e16cd3a704e264ea8f5e24f548db',
+ 'multinomial': '7a984ae6dca26fd25374479e118b22f55db0aedccd5a0f2584ceada33db98605',
+ 'negative_binomial': 'd636d968e6a24ae92ab52fe11c46ac45b0897e98714426764e820a7d77602a61',
+ 'poisson': '956552176f77e7c9cb20d0118fc9cf690be488d790ed4b4c4747b965e61b0bb4',
+ 'zipf': 'f84ba7feffda41e606e20b28dfc0f1ea9964a74574513d4a4cbc98433a8bfa45',
}
@@ -319,18 +319,18 @@ class TestRandint:
assert_(vals.min() >= 0)
def test_repeatability(self):
- # We use a md5 hash of generated sequences of 1000 samples
+ # We use a sha256 hash of generated sequences of 1000 samples
# in the range [0, 6) for all but bool, where the range
# is [0, 2). Hashes are for little endian numbers.
- tgt = {'bool': '7dd3170d7aa461d201a65f8bcf3944b0',
- 'int16': '1b7741b80964bb190c50d541dca1cac1',
- 'int32': '4dc9fcc2b395577ebb51793e58ed1a05',
- 'int64': '17db902806f448331b5a758d7d2ee672',
- 'int8': '27dd30c4e08a797063dffac2490b0be6',
- 'uint16': '1b7741b80964bb190c50d541dca1cac1',
- 'uint32': '4dc9fcc2b395577ebb51793e58ed1a05',
- 'uint64': '17db902806f448331b5a758d7d2ee672',
- 'uint8': '27dd30c4e08a797063dffac2490b0be6'}
+ tgt = {'bool': '509aea74d792fb931784c4b0135392c65aec64beee12b0cc167548a2c3d31e71',
+ 'int16': '7b07f1a920e46f6d0fe02314155a2330bcfd7635e708da50e536c5ebb631a7d4',
+ 'int32': 'e577bfed6c935de944424667e3da285012e741892dcb7051a8f1ce68ab05c92f',
+ 'int64': '0fbead0b06759df2cfb55e43148822d4a1ff953c7eb19a5b08445a63bb64fa9e',
+ 'int8': '001aac3a5acb935a9b186cbe14a1ca064b8bb2dd0b045d48abeacf74d0203404',
+ 'uint16': '7b07f1a920e46f6d0fe02314155a2330bcfd7635e708da50e536c5ebb631a7d4',
+ 'uint32': 'e577bfed6c935de944424667e3da285012e741892dcb7051a8f1ce68ab05c92f',
+ 'uint64': '0fbead0b06759df2cfb55e43148822d4a1ff953c7eb19a5b08445a63bb64fa9e',
+ 'uint8': '001aac3a5acb935a9b186cbe14a1ca064b8bb2dd0b045d48abeacf74d0203404'}
for dt in self.itype[1:]:
random.seed(1234)
@@ -341,13 +341,13 @@ class TestRandint:
else:
val = self.rfunc(0, 6, size=1000, dtype=dt).byteswap()
- res = hashlib.md5(val.view(np.int8)).hexdigest()
+ res = hashlib.sha256(val.view(np.int8)).hexdigest()
assert_(tgt[np.dtype(dt).name] == res)
# bools do not depend on endianness
random.seed(1234)
val = self.rfunc(0, 2, size=1000, dtype=bool).view(np.int8)
- res = hashlib.md5(val).hexdigest()
+ res = hashlib.sha256(val).hexdigest()
assert_(tgt[np.dtype(bool).name] == res)
@pytest.mark.skipif(np.iinfo('l').max < 2**32,
@@ -1974,7 +1974,7 @@ class TestSingleEltArrayInput:
# Ensure returned array dtype is correct for platform
def test_integer_dtype(int_func):
random.seed(123456789)
- fname, args, md5 = int_func
+ fname, args, sha256 = int_func
f = getattr(random, fname)
actual = f(*args, size=2)
assert_(actual.dtype == np.dtype('l'))
@@ -1982,10 +1982,20 @@ def test_integer_dtype(int_func):
def test_integer_repeat(int_func):
random.seed(123456789)
- fname, args, md5 = int_func
+ fname, args, sha256 = int_func
f = getattr(random, fname)
val = f(*args, size=1000000)
if sys.byteorder != 'little':
val = val.byteswap()
- res = hashlib.md5(val.view(np.int8)).hexdigest()
- assert_(res == md5)
+ res = hashlib.sha256(val.view(np.int8)).hexdigest()
+ assert_(res == sha256)
+
+
+def test_broadcast_size_error():
+ # GH-16833
+ with pytest.raises(ValueError):
+ random.binomial(1, [0.3, 0.7], size=(2, 1))
+ with pytest.raises(ValueError):
+ random.binomial([1, 2], 0.3, size=(2, 1))
+ with pytest.raises(ValueError):
+ random.binomial([1, 2], [0.3, 0.7], size=(2, 1))
diff --git a/numpy/random/tests/test_randomstate_regression.py b/numpy/random/tests/test_randomstate_regression.py
index 4eb82fc4c..0bf361e5e 100644
--- a/numpy/random/tests/test_randomstate_regression.py
+++ b/numpy/random/tests/test_randomstate_regression.py
@@ -44,11 +44,11 @@ class TestRegression:
# numbers with this large sample
# theoretical large N result is 0.49706795
freq = np.sum(rvsn == 1) / float(N)
- msg = "Frequency was %f, should be > 0.45" % freq
+ msg = f'Frequency was {freq:f}, should be > 0.45'
assert_(freq > 0.45, msg)
# theoretical large N result is 0.19882718
freq = np.sum(rvsn == 2) / float(N)
- msg = "Frequency was %f, should be < 0.23" % freq
+ msg = f'Frequency was {freq:f}, should be < 0.23'
assert_(freq < 0.23, msg)
def test_shuffle_mixed_dimension(self):
diff --git a/numpy/random/tests/test_regression.py b/numpy/random/tests/test_regression.py
index 278622287..54d5a3efb 100644
--- a/numpy/random/tests/test_regression.py
+++ b/numpy/random/tests/test_regression.py
@@ -40,11 +40,11 @@ class TestRegression:
# numbers with this large sample
# theoretical large N result is 0.49706795
freq = np.sum(rvsn == 1) / float(N)
- msg = "Frequency was %f, should be > 0.45" % freq
+ msg = f'Frequency was {freq:f}, should be > 0.45'
assert_(freq > 0.45, msg)
# theoretical large N result is 0.19882718
freq = np.sum(rvsn == 2) / float(N)
- msg = "Frequency was %f, should be < 0.23" % freq
+ msg = f'Frequency was {freq:f}, should be < 0.23'
assert_(freq < 0.23, msg)
def test_shuffle_mixed_dimension(self):
diff --git a/numpy/random/tests/test_smoke.py b/numpy/random/tests/test_smoke.py
index ebfc6825e..909bfaa8d 100644
--- a/numpy/random/tests/test_smoke.py
+++ b/numpy/random/tests/test_smoke.py
@@ -129,7 +129,7 @@ class RNG:
assert_(not comp_state(state, self.rg.bit_generator.state))
else:
bitgen_name = self.rg.bit_generator.__class__.__name__
- pytest.skip('Advance is not supported by {0}'.format(bitgen_name))
+ pytest.skip(f'Advance is not supported by {bitgen_name}')
def test_jump(self):
state = self.rg.bit_generator.state
@@ -145,8 +145,8 @@ class RNG:
else:
bitgen_name = self.rg.bit_generator.__class__.__name__
if bitgen_name not in ('SFC64',):
- raise AttributeError('no "jumped" in %s' % bitgen_name)
- pytest.skip('Jump is not supported by {0}'.format(bitgen_name))
+ raise AttributeError(f'no "jumped" in {bitgen_name}')
+ pytest.skip(f'Jump is not supported by {bitgen_name}')
def test_uniform(self):
r = self.rg.uniform(-1.0, 0.0, size=10)
@@ -447,8 +447,7 @@ class RNG:
def test_seed_array(self):
if self.seed_vector_bits is None:
bitgen_name = self.bit_generator.__name__
- pytest.skip('Vector seeding is not supported by '
- '{0}'.format(bitgen_name))
+ pytest.skip(f'Vector seeding is not supported by {bitgen_name}')
if self.seed_vector_bits == 32:
dtype = np.uint32