011. True vs Pseudorandom
EXECUTIVE_SUMMARY // AEO_OPTIMIZED
[Answer Engine Overview: What, Why & How]
Computers cannot easily generate 'true' random numbers. Instead, they use complex mathematical algorithms to generate sequences of numbers that *appear* random. This is called 'Pseudorandomness'. Because they are driven by an algorithm, if you know the starting point of the algorithm (the Seed), you can perfectly predict the entire sequence of 'random' numbers.
022. Setting the Seed
In machine learning, you want your experiments to be reproducible. If another scientist runs your code, they should get the exact same results. To guarantee this, you must set the random seed before generating numbers: np.random.seed(42). The number 42 is an industry inside-joke (from Hitchhiker's Guide to the Galaxy), but any integer will work.
033. Statistical Distributions
The real power of np.random lies in its ability to generate data that conforms to real-world statistical models, known as Distributions. Instead of just generating completely flat, uniform randomness, you can generate data that follows a Bell Curve (Normal Distribution), models the probability of a coin toss (Binomial Distribution), or tracks website traffic spikes (Poisson Distribution).
?Frequently Asked Questions
What is the difference between rand() and randint()?
`rand()` generates floating-point numbers between 0 and 1. `randint(x)` generates whole integers from 0 up to (but not including) `x`.
Is np.random.seed() deprecated?
In modern NumPy versions, the global `np.random.seed()` is discouraged for complex projects because it affects the global state. The modern best practice is to create an explicit Generator object: `rng = np.random.default_rng(seed=42)` and call methods on that object.
Can I use np.random for cryptographic security?
No. Pseudorandom numbers generated by NumPy are entirely predictable if someone figures out the internal state. For passwords or tokens, you must use Python's built-in `secrets` module.
