Page6/12
Randomness & Simulation · Page 1 of 1
The Random Module
Randomness & Simulation
Why Randomness?
Machine learning relies heavily on randomness: initializing weights, splitting data into train/test sets, and dropout regularization.
Setting the Seed
Computers generate pseudo-random numbers. Setting a seed ensures reproducibility:
np.random.seed(42) # Always generates the same "random" numbers
Essential Random Functions
np.random.rand(d1, d2): Uniform distribution [0, 1)np.random.randn(d1, d2): Standard Normal (Gaussian) distribution (mean=0, std=1)np.random.randint(low, high): Random integersnp.random.choice(array): Random sample from an array
Simulating the Central Limit Theorem (CLT)
The CLT states that if you take enough random samples from any distribution, the means of those samples will form a normal distribution. We can prove this easily with NumPy!
main.py
Loading...
OUTPUT
▶Click "Run Code" to execute…