Why random generation matters in NumPy - Performance Analysis
We want to see how the time to create random numbers changes as we ask for more numbers.
How does the work grow when we generate bigger random arrays?
Analyze the time complexity of the following code snippet.
import numpy as np
n = 1000
random_numbers = np.random.rand(n)
This code creates an array of n random numbers between 0 and 1.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Generating each random number.
- How many times: Exactly
ntimes, once for each number.
As we ask for more random numbers, the work grows in a straight line with the size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 random number generations |
| 100 | About 100 random number generations |
| 1000 | About 1000 random number generations |
Pattern observation: Doubling the input roughly doubles the work needed.
Time Complexity: O(n)
This means the time to generate random numbers grows directly with how many numbers you want.
[X] Wrong: "Generating many random numbers is almost free and does not depend on how many I ask for."
[OK] Correct: Each random number requires work, so more numbers mean more time.
Understanding how random number generation scales helps you explain performance when working with simulations or data sampling.
"What if we generate a 2D array of random numbers instead of 1D? How would the time complexity change?"