Normal distribution with normal() in NumPy - Time & Space Complexity
We want to understand how the time to create random numbers from a normal distribution changes as we ask for more numbers.
How does the work grow when we increase the amount of data generated?
Analyze the time complexity of the following code snippet.
import numpy as np
# Generate 1 million random numbers from a normal distribution
samples = np.random.normal(loc=0, scale=1, size=1000000)
This code creates a large array of random numbers following a bell curve shape centered at 0.
Look for repeated work inside the code.
- Primary operation: Generating each random number independently.
- How many times: Once for each number requested (here, 1 million times).
When you ask for more numbers, the work grows in a straight line with the amount you want.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 random numbers generated |
| 100 | 100 random numbers generated |
| 1000 | 1000 random numbers generated |
Pattern observation: Doubling the input size doubles the work needed.
Time Complexity: O(n)
This means the time to generate numbers grows directly in proportion to how many numbers you want.
[X] Wrong: "Generating 1 million numbers is just as fast as generating 10 because computers are fast."
[OK] Correct: Even though computers are fast, each number takes some time to create, so more numbers mean more total time.
Understanding how generating random data scales helps you reason about performance in simulations and data analysis tasks.
"What if we generate a 2D array of random numbers instead of 1D? How would the time complexity change?"