Arrays - random number generating

NumPy - The Basics

2 min read

Published Sep 22 2025


11
0
0
0

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) and high (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 only size as 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 low and high.
  • 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 = mean
    • scale = 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.random functions 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 = 20
np.random.seed(20)
print(np.random.randint(250, 888, size=(4, 3)))
# → (matrix A)

# Seed = 10
np.random.seed(10)
print(np.random.randint(250, 888, size=(4, 3)))
# → (matrix B, different from A)

# Seed = 10 again
np.random.seed(10)
print(np.random.randint(250, 888, size=(4, 3)))
# → same as (matrix B)

Products from our shop

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Mug

Docker Cheat Sheet Mug

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Mug

Vim Cheat Sheet Mug

SimpleSteps.guide branded Travel Mug

SimpleSteps.guide branded Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - Black

Developer Excuse Javascript Mug - Black

SimpleSteps.guide branded stainless steel water bottle

SimpleSteps.guide branded stainless steel water bottle

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Dark

Developer Excuse Javascript Hoodie - Dark

© 2025 SimpleSteps.guide
AboutFAQPoliciesContact