8.3. NumPy Randomness#

Topic

Legacy (global RNG)

Modern (Generator API)

Main objects

np.random module, RandomState

np.random.Generator (e.g., rng = default_rng(seed))

Seeding

np.random.seed(123) (global)

rng = np.random.default_rng(123) (local, explicit)

State scope

Global, shared across code

Isolated per rng, easy to pass around

Typical calls

np.random.rand, randn, randint, random_sample

rng.random, normal, integers, uniform, …

Reproducibility

Fragile (any call anywhere advances global state)

Robust (each rng advances its own state only)

Performance/algorithms

Older (MT19937); OK speed

Newer bit-generators (PCG64 default), often faster/better

Broadcasting/shape

Functions accept size= or separate dims (rand(2,3))

Consistent size= tuple; parameters broadcast cleanly

Thread/process safety

Global contention possible

Use separate Generators per thread/process

Legacy

Modern (preferred)

np.random.seed(123)

rng = np.random.default_rng(123)

np.random.rand(2,3)

rng.random((2,3))

np.random.randn(5)

rng.normal(loc=0, scale=1, size=5)

np.random.randint(0, 10, 4)

rng.integers(0, 10, size=4) (note: high exclusive)

np.random.random_sample(3) / np.random.sample(3)

rng.random(3)

np.random.uniform(-1, 1, 100)

rng.uniform(-1, 1, size=100)

np.random.binomial(n=10, p=0.3, size=6)

rng.binomial(10, 0.3, size=6)

np.random.poisson(lam=5, size=(3,3))

rng.poisson(5, size=(3,3))

np.random.permutation(n)

rng.permutation(n)

np.random.shuffle(a)

rng.shuffle(a)

Call / Purpose

Legacy (global RNG)

Modern (Generator API)

Example (legacy → modern)

Seed

random.seed(123)

rng = random.default_rng(123)

random.seed(123)rng = random.default_rng(123)

Uniform floats

random.rand(2, 3)

rng.random((2, 3))

random.rand(2,3)rng.random((2,3))

Standard normal

random.randn(5)

rng.normal(0, 1, size=5)

random.randn(5)rng.normal(0,1,5)

Integers

random.randint(0, 10, 4)

rng.integers(0, 10, size=4)

random.randint(0,10,4)rng.integers(0,10,4) (high is exclusive)

Random sample / sample alias

random.random_sample(3) (= random.sample(3))

rng.random(3)

random.random_sample(3)rng.random(3)

Uniform in [low, high)

random.uniform(-1, 1, 6)

rng.uniform(-1, 1, size=6)

random.uniform(-1,1,6)rng.uniform(-1,1,6)

Binomial

random.binomial(10, 0.3, size=6)

rng.binomial(10, 0.3, size=6)

random.binomial(10,0.3,6)rng.binomial(10,0.3,6)

Poisson

random.poisson(5, size=(3, 3))

rng.poisson(5, size=(3,3))

random.poisson(5,(3,3))rng.poisson(5,(3,3))

Permutation (returns permuted copy)

random.permutation(10)

rng.permutation(10)

random.permutation(10)rng.permutation(10)

Shuffle (in place)

random.shuffle(a)

rng.shuffle(a)

random.shuffle(a)rng.shuffle(a)

8.3.1. Array Attributes and Methods#

  • Randomness: rand, randn, randint

arr = np.arange(25)
ranarr = np.random.randint(0,50,10)
arr
ranarr

8.3.2. Random#

Numpy also has lots of ways to create random number arrays:

8.3.2.1. rand#

Create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1).

np.random.rand(5)
np.random.rand(5,5)

8.3.2.2. randn#

Return a sample (or samples) from the “standard normal” distribution. Unlike rand which is uniform:

np.random.randn(5)
np.random.randn(5,5)

8.3.2.3. randint#

Return random integers from low (inclusive) to high (exclusive).

np.random.randint(1,100)
np.random.randint(1,100,10)