Generate random integers, uniform floats, standard normal distributions, and lock seeds.
The `np.random` module provides high-speed pseudo-random number generation for simulations, statistical sampling, and machine learning weight initialization.
A random seed is the bookmark in an infinite mathematical phonebook of deterministic numbers. Setting the same seed reads the exact same pages every time.
`rand()` generates uniform floats in [0, 1). `randint(low, high)` generates discrete integers. `randn()` samples standard normal Gaussian distribution (mean=0, std=1).
np.random.seed(42); arr = np.random.randint(1, 10, size=5)np.random.rand(d0, d1, ...)Floats (Dimensions)Continuous uniform probability distribution across unit interval
np.random.randn(d0, d1, ...)Bell CurveStandard normal distribution bell curve (can produce negative values)
np.random.randint(low, high, size)IntegersSample discrete integers (high endpoint excluded)
np.random.random(size)Tuple ShapeContinuous uniform float sampling accepting a shape tuple as argument
np.random.choice(a, size)Discrete SamplingSample random items with or without replacement from an input vector
np.random.shuffle(arr)In-Place MutateModifies existing array directly in memory without returning a new object
np.random.permutation(x)Returns New CopyReturns a fresh randomly permuted sequence or array copy
np.random.uniform(low, high, size)Bounded RangeContinuous floats sampled uniformly between custom low & high bounds
np.random.normal(loc, scale, size)Custom GaussianNormal distribution with custom center loc (μ) and spread scale (σ)
np.random.seed(42)Seed LockEnsures generated random values are 100% identical on every run
np.random.randint(1, 10) # generates 1 scalar instead of arraynp.random.randint(1, 10, size=5)Provide `size=N` to generate an array of numbers instead of a single integer.
Solve the objective below using NumPy vectorized syntax
Set seed to 42, generate 5 random integers between 1 and 20 with `size=5`, and print them.
[ 7 15 11 8 7]