https://docs.scipy.org/doc/numpy-1.16.0/reference/routines.random.html
Random sampling (numpy.random)
https://numpy.org/doc/stable/reference/random/index.html
Random sampling (numpy.random) for numpy versions > 1.17
https://numpy.org/doc/stable/reference/random/new-or-different.html
BitGenerator (PCG64 is the default) | (stream of bits) Generator | (distributions) pseudo-random numbers
The numpy.random module implements pseudo-random number generators (RNGs) with the ability to draw samples from a variety of probability distributions. In general, users will create a Generator instance with default_rng and call the various methods on it to obtain samples from different distributions.
Each Generator instance owns a BitGenerator instance that implements the core RNG algorithm. The Generator takes the bit generator-provided stream and transforms them into more useful distributions.
NumPy implements several different BitGenerator classes implementing different RNG algorithms. default_rng currently uses PCG64 as the default BitGenerator. There is also the MT19937 (Mersenne Twister) algorithm used in the legacy RandomState class. RandomState is maintained for backward compatibility.
Many of the RandomState methods are exported as functions in numpy.random. This usage is discouraged, as it is implemented via a global RandomState instance.
import numpy as np # Recommended constructor for Generator. # numpy.random.default_rng(seed=None) rng = np.random.default_rng() print(rng) # Generator(PCG64) # Generate random floats uniformly distributed over the range [0,1). rng.random() # 0.17587875075447124 # may vary rng.random((2,3)) # shape=(2,3) # array([[0.29460014, 0.15512146, 0.3207094 ], # [0.67896005, 0.19816737, 0.79222467]]) # Generate an array of 10 numbers according to a unit Gaussian distribution. rng.standard_normal(10) # array([-0.83337107, -1.11861209, -0.28787136, 0.83640375, 0.07775067, # 0.33662937, 0.10654067, 0.44827837, -0.09803265, -0.73633881]) # Generate an array of 5 integers uniformly over the range [0,10). rng.integers(low=0, high=10, size=5) # array([7, 1, 3, 8, 8]) # may vary # Generate random bytes. rng.bytes(8) # lenght=8 # b'r\x1c2o\xe5\xdf\x1a\xdb'
# Simple random data. np.random.rand() # return a random float from [0,1) (a uniform distribution) np.random.rand(d0,d1,...,dn) # return an array, shape=(d0,d1,...,dn) np.random.random_sample() np.random.random_sample((d0,d1,...,dn)) np.random.random() # alias for np.random.random_sample()
np.random.randn() # return a sample from the 'standard normal' distribution np.random.randn(d0,d1,...,dn) # return an array, shape=(d0,d1,...,dn) np.random.standard_normal(size=1000) # mu=0, sigma=1 np.random.standard_normal(size=(d0,d1,...,dn)) # shape=(d0,d1,...,dn)
# np.random.randint(low, high=None, size=None, dtype=int) # size=10, size=(2,4), return size-shaped array np.random.randint(high) # random integers from [0, high) np.random.randint(low, high) # random integers from [low, high) np.random.randint(1,7,10) # array([6, 3, 3, 3, 6, 3, 1, 1, 2, 2]) np.random.randint(5, size=(2, 4)) # array([[4, 0, 2, 1], # [3, 2, 2, 0]])
# np.random.choice(a, size=None, replace=True, p=None) # a : 1-D array-like or int # size : output shape (int or tuple of ints) # replace : boolean, whether the sample is with or without replacement # p : the probabilities associated with each entry in a np.random.choice(arr) # return one item from arr np.random.choice(8) # like np.random.choice(np.arange(8)) np.random.choice(list_four_strings, size=6, p=[0.5, 0.1, 0.1, 0.3]) np.random.choice(arr, replace=False) # every item at most once np.random.choice(range(1,7), size=(2,3)) # array([[1, 5, 4], # [5, 6, 2]])
# Permutations np.random.shuffle(arr) # operates in-place np.random.permutation(arr) # returns a copy np.random.permutation(n) # np.random.permutation(np.arange(n))
# Distributions mu, sigma, size = 0, 0.1, 1000 # mean and standard deviation s = np.random.normal(mu, sigma, size) n, p, size = 10, .5, 1000 # number of trials, probability of each trial s = np.random.binomial(n, p, size) # result of flipping a coin 10 times, tested 1000 times. loc, scale, size = 10, 1, 1000 s = np.random.logistic(loc, scale, size) low, high, size = -1, 1, 1000 s = np.random.uniform(low, high, size) # interval [low, high) s = np.random.uniform((-1,-1), (1,1), (100,2)) # 100 points from the square [-1,1)x[-1,1) left, mode, right, size = -3, 0, 8, 1000 s = np.random.triangular(left, mode, right, size) # interval [left, right] s = np.random.poisson(lam=5, size=1000) s = np.random.exponential(scale=1.0, size=1000) loc, scale, size = 0., 1., 1000 s = np.random.laplace(loc, scale, size)
# Random generator # seed must be convertible to 32 bit unsigned integers np.random.seed(42)