3.3. Random Number Generation#
3.3.1. The numpy.random module#
(These functions discussed in this section are considered “legacy random generation ” and are for simple random data and for instructional purposes. The next section provides some information about the new random modules in numpy for your information.)
The numpy.random module in NumPy provides tools for generating pseudo-random numbers and sampling from various probability distributions. Some functions in the numpy.random module are commonly used:
np.random.rand(): returns random floating-point numbers (uniform distribution over the interval [0, 1))
np.random.randn(): returns random floating-point numbers (standard normal distribution with mean of 0 and standard deviation of 1) (note that np.random.random is the same as random.rand(), except random.random() takes the shape argument as a single tuple; e.g., see stackoverflow )
np.random.randint(): returns random integers from a specified range
As a summary, here’s a comparison table of the commonly used methods in the numpy random module:
Function |
Distribution |
Typical Use |
Note |
Example |
|---|---|---|---|---|
|
Uniform [0,1) |
Random decimals |
uniform decimals |
|
|
Uniform [0,1) |
Same as rand |
uniform decimals |
|
|
Normal (0,1) |
Stats, ML |
bell curve; mean=0, SD=1 |
|
|
Normal (μ, σ) |
Simulations |
bell curve; mean & SD controllable |
|
|
Integers |
Dice, indexing |
whole numbers |
|
Simple examples of the random numbers are:
3.3.1.1. random.rand( )#
np.random.rand() ### rand: one number
np.random.rand(3) ### rand: 1D array
np.random.rand(2, 3) ### rand: 2D
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[1], line 1
----> 1 np.random.rand() ### rand: one number
2 np.random.rand(3) ### rand: 1D array
3 np.random.rand(2, 3) ### rand: 2D
NameError: name 'np' is not defined
3.3.1.2. random.random()#
np.random.random() ### random: one number
np.random.random(3) ### random: 1D array
np.random.random((2, 3)) ### random: 2D
np.random.random(size=(2, 3)) ### random: 2D
0.39189636217351265
3.3.1.3. random.randn( )#
np.random.randn() ### randn: 1 number, normal distribution
np.random.randn(3) ### randn: 1D array, normal distribution
np.random.randn(2, 3) ### randn: 2D array, normal distribution
# np.random.randn(2, 3, 4) ### randn: 3D array, normal distribution
# np.random.randn(2, 5, 2, 3) ### randn: 4D array, normal distribution
array([[-0.54916384, -2.15880801, -1.77908345],
[ 1.77800311, -1.66655901, 0.18373012]])
3.3.1.4. random.normal()#
np.random.normal(loc=0.0, scale=1.0, size=None)
loc = location of mean
np.random.normal() ### normal: one number, normal distribution
np.random.normal(3) ### normal: one number, mean=3
np.random.normal(0, 1) ### normal: mean=0, std=1
np.random.normal(loc=0, scale=1) ### normal: mean=0, std=1
np.random.normal(0, 1, (2, 3)) ### normal: mean=0, std=1, shape=(2,3)
# create a 3x3 array of normally distributed pseudorandom
# values with mean 0 and standard deviation 1
array([[ 0.40773917, -0.63680053, -1.05366199],
[-0.61409056, 1.75777194, -0.57610549]])
3.3.1.5. random.randint( )#
np.random.randint( 10 ) ### 0 to 10 exclusive
np.random.randint( 5, 10 ) ### 5 to 10 exclusive
np.random.randint(5, 10, (3, 3)) ### normal: mean=0, std=1, shape=(3,3)
array([[9, 6, 5],
[7, 6, 9],
[5, 9, 9]])
3.3.1.6. choice( )#
np.random.choice([1, 2, 3, 4, 5], size = 2) ### the choice function
array([1, 1])
3.3.1.7. size#
np.random.choice([3, 5, 7, 9], size=(3, 5)) ### the size parameter to generate N-D arrays
array([[3, 3, 3, 9, 9],
[7, 7, 7, 9, 9],
[5, 3, 5, 3, 9]])
This section is for informational purposes only.
This section compares NumPy’s legacy random number generator with modern Generator API for reproducible randomness.
Topic |
Legacy (global RNG) |
Modern (Generator API) |
|---|---|---|
Main objects |
|
|
Seeding |
|
|
State scope |
Global, shared across code |
Isolated per |
Typical calls |
|
|
Reproducibility |
Fragile (any call anywhere advances global state) |
Robust (each |
Performance/algorithms |
Older (MT19937); OK speed |
Newer bit-generators (PCG64 default), often faster/better |
Broadcasting/shape |
Functions accept |
Consistent |
Thread/process safety |
Global contention possible |
Use separate |
Legacy |
Modern (preferred) |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Call / Purpose |
Legacy (global RNG) |
Modern (Generator API) |
Example (legacy → modern) |
|---|---|---|---|
Seed |
|
|
|
Uniform floats |
|
|
|
Standard normal |
|
|
|
Integers |
|
|
|
Random sample / sample alias |
|
|
|
Uniform in [low, high) |
|
|
|
Binomial |
|
|
|
Poisson |
|
|
|
Permutation (returns permuted copy) |
|
|
|
Shuffle (in place) |
|
|
|