Arrays - random number generating
NumPy - The Basics
2 min read
This section is 2 min read, full guide is 14 min read
Published Sep 22 2025, updated Aug 17 2026
11
Show sections list
0
Log in to enable the "Like" button
0
Guide comments
0
Log in to enable the "Save" button
Respond to this guide
Guide Sections
Guide Comments
NumPyPython
Random Data in NumPy
NumPy provides several functions in numpy.random to generate arrays with random numbers from different distributions.
Uniform Distribution (0 to 1)
np.random.rand(2, 2)- Generates numbers in the range
[0, 1). - Uniformly distributed.
- Shape defined by arguments.
Example:
np.random.rand(2, 2)# → [[0.65 0.12]# [0.44 0.89]]Standard Normal Distribution
np.random.randn(8, 2)- Generates values from a standard normal distribution (mean = 0, std = 1).
- Useful for simulations and ML initialisation.
Example:
np.random.randn(3, 3)# → [[ 0.12 -0.89 0.44]# [ 1.45 0.33 -0.55]# [-0.23 0.78 -1.09]]Random Integers
np.random.randint(low=250, high=888, size=(4, 3))- Generates random integers between
low(inclusive) andhigh(exclusive). - Shape defined by
size.
Example:
np.random.randint(10, 20, size=(2, 5))# → [[14 18 12 11 17]# [19 10 15 12 13]]Random Sample from [0, 1)
np.random.random(size=(3, 2))- Similar to
rand(), but takes onlysizeas a tuple.
Example:
np.random.random((3, 2))# → [[0.42 0.73]# [0.89 0.15]# [0.65 0.22]]Random Choice
np.random.choice([1, 5, 9], size=(2, 3))- Randomly selects values from a given list/array.
- Useful for categorical or discrete sampling.
Example:
np.random.choice([10, 20, 30], size=5)# → [30 20 10 20 30]Uniform Distribution
np.random.uniform(low=5, high=15, size=(3, 3))- Random floats between
lowandhigh. - Uniform distribution.
Example:
np.random.uniform(5, 10, size=(2, 4))# → [[5.77 6.12 9.45 7.01]# [8.22 9.88 5.66 6.99]]Normal Distribution (custom mean & std)
np.random.normal(loc=50, scale=10, size=(3, 3))- Random floats from a normal distribution with:
loc= meanscale= standard deviation
Example:
np.random.normal(0, 1, size=5)# → [-0.23 0.87 -1.12 0.44 0.19]Seeding Random Numbers
Random numbers in NumPy are pseudo-random (deterministic, generated by an algorithm).
np.random.seed(1)arr = np.random.randint(10, 50, size=5)print(arr)# → [37 12 29 37 18] (always the same with seed=1)np.random.seed(seed)fixes the sequence → ensures reproducibility.- If you set the same seed, you’ll always get the same "random" output.
- Useful for debugging, testing, and reproducible research.
How np.random.seed(seed) Works:
- All the
np.randomfunctions above (rand,randn,randint,random,uniform,normal,choice, etc.) pull numbers from the same underlying random number generator. - When you call
np.random.seed(seed), you reset that generator to a fixed state. - That means:
- Using the same seed → you’ll get the same sequence of random numbers.
- Changing the seed → produces a different sequence.
- Resetting to a previous seed → reproduces the exact same values again.
Example:
import numpy as np# Seed = 20np.random.seed(20)print(np.random.randint(250, 888, size=(4, 3)))# → (matrix A)# Seed = 10np.random.seed(10)print(np.random.randint(250, 888, size=(4, 3)))# → (matrix B, different from A)# Seed = 10 againnp.random.seed(10)print(np.random.randint(250, 888, size=(4, 3)))# → same as (matrix B)