Random numbers in NumPy
Contents
Random numbers in NumPy¶
Random integers¶
Here is the recommended way to make random integers in NumPy. We first instantiate a “random number generator” that we call rng
.
import numpy as np
rng = np.random.default_rng()
help(rng.integers)
Help on built-in function integers:
integers(...) method of numpy.random._generator.Generator instance
integers(low, high=None, size=None, dtype=np.int64, endpoint=False)
Return random integers from `low` (inclusive) to `high` (exclusive), or
if endpoint=True, `low` (inclusive) to `high` (inclusive). Replaces
`RandomState.randint` (with endpoint=False) and
`RandomState.random_integers` (with endpoint=True)
Return random integers from the "discrete uniform" distribution of
the specified dtype. If `high` is None (the default), then results are
from 0 to `low`.
Parameters
----------
low : int or array-like of ints
Lowest (signed) integers to be drawn from the distribution (unless
``high=None``, in which case this parameter is 0 and this value is
used for `high`).
high : int or array-like of ints, optional
If provided, one above the largest (signed) integer to be drawn
from the distribution (see above for behavior if ``high=None``).
If array-like, must contain integer values
size : int or tuple of ints, optional
Output shape. If the given shape is, e.g., ``(m, n, k)``, then
``m * n * k`` samples are drawn. Default is None, in which case a
single value is returned.
dtype : dtype, optional
Desired dtype of the result. Byteorder must be native.
The default value is np.int64.
endpoint : bool, optional
If true, sample from the interval [low, high] instead of the
default [low, high)
Defaults to False
Returns
-------
out : int or ndarray of ints
`size`-shaped array of random integers from the appropriate
distribution, or a single such random int if `size` not provided.
Notes
-----
When using broadcasting with uint64 dtypes, the maximum value (2**64)
cannot be represented as a standard integer type. The high array (or
low if high is None) must have object dtype, e.g., array([2**64]).
Examples
--------
>>> rng = np.random.default_rng()
>>> rng.integers(2, size=10)
array([1, 0, 0, 0, 1, 1, 0, 0, 1, 0]) # random
>>> rng.integers(1, size=10)
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
Generate a 2 x 4 array of ints between 0 and 4, inclusive:
>>> rng.integers(5, size=(2, 4))
array([[4, 0, 2, 1],
[3, 2, 2, 0]]) # random
Generate a 1 x 3 array with 3 different upper bounds
>>> rng.integers(1, [3, 5, 10])
array([2, 2, 9]) # random
Generate a 1 by 3 array with 3 different lower bounds
>>> rng.integers([1, 5, 7], 10)
array([9, 8, 7]) # random
Generate a 2 by 4 array using broadcasting with dtype of uint8
>>> rng.integers([1, 3, 5, 7], [[10], [20]], dtype=np.uint8)
array([[ 8, 6, 9, 7],
[ 1, 16, 9, 12]], dtype=uint8) # random
References
----------
.. [1] Daniel Lemire., "Fast Random Integer Generation in an Interval",
ACM Transactions on Modeling and Computer Simulation 29 (1), 2019,
http://arxiv.org/abs/1805.10941.
Making a 10x2 NumPy array of random integers between 1 (inclusive) and 5 (exclusive).
rng.integers(1,5,size=(10,2))
array([[1, 4],
[4, 2],
[2, 1],
[2, 4],
[3, 1],
[2, 2],
[1, 2],
[4, 1],
[4, 4],
[4, 1]])
Here are two ways to include 5.
rng.integers(1,6,size=(10,2))
array([[1, 5],
[2, 3],
[2, 4],
[5, 5],
[2, 2],
[4, 2],
[1, 4],
[4, 2],
[1, 1],
[4, 2]])
rng.integers(1,5,size=(10,2),endpoint=True)
array([[4, 2],
[4, 5],
[1, 4],
[5, 3],
[3, 4],
[2, 2],
[2, 2],
[4, 5],
[4, 4],
[2, 2]])
Random real numbers¶
If making random real numbers, the range is always between 0 and 1; there is no way to specify the upper and lower bounds as inputs to the function. So to increase the range of outputs, you multiply, and to shift the range of outputs, you add.
rng.random(size=(10,2))
array([[0.80134458, 0.99640117],
[0.85749254, 0.2708051 ],
[0.89570258, 0.07247259],
[0.57586932, 0.02725423],
[0.72336617, 0.14831277],
[0.97474321, 0.33677344],
[0.02454157, 0.75908029],
[0.19539268, 0.57847429],
[0.4489261 , 0.54827182],
[0.86845109, 0.18691298]])
Random real numbers between 0 and 30:
30*rng.random(size=(10,2))
array([[ 2.76976422, 5.69854339],
[ 3.98745051, 9.12122506],
[ 6.33166223, 9.34835658],
[16.75918019, 12.0920213 ],
[21.61374081, 14.18795008],
[28.99225637, 9.01313128],
[27.99941972, 27.98573676],
[ 4.70563762, 8.0592845 ],
[24.46489305, 27.16596474],
[22.64248123, 11.61230701]])
Random real numbers between 5 and 35:
5 + 30*rng.random(size=(10,2))
array([[14.47396729, 26.44411472],
[ 9.10239787, 7.25839204],
[31.24764063, 32.6178989 ],
[16.06201993, 19.05997337],
[14.98403114, 20.70614716],
[28.93956636, 29.7784064 ],
[26.16701256, 9.34524409],
[14.33832551, 15.19518886],
[19.14548091, 20.43675888],
[13.47944194, 5.48535503]])