Numpy Random Number Generation: A Beginner's Guide to rand and randn
NumPy is the core library for scientific computing in Python. The `np.random` submodule provides random number generation functionality, with `rand` and `randn` being commonly used functions. These random numbers are pseudo-random, and fixing the seed allows for reproducible results. `np.random.rand(d0, …, dn)` generates random numbers from a **uniform distribution over [0, 1)**. The parameters specify the array shape (e.g., 1-dimensional, 2-dimensional, etc.), and all elements lie within [0, 1). It is suitable for scenarios requiring equal probability values (e.g., initializing weights). `np.random.randn(d0, …, dn)` generates random numbers from a **standard normal distribution** (mean 0, standard deviation 1). Elements are concentrated between -1 and 1, with a low probability of extreme values. To adjust the mean and standard deviation, the formula `μ + σ * randn` can be used. This is often applied to simulate natural data fluctuations (e.g., noise). Both functions accept shape parameters, with the former producing uniform distribution and the latter normal distribution. The results can be reproduced by fixing the seed using `np.random.seed(seed)`.
Read More