np.random.default_rng() modern approach in NumPy - Time & Space Complexity
We want to understand how the time it takes to generate random numbers grows as we ask for more numbers using np.random.default_rng().
How does the work increase when we generate bigger arrays of random numbers?
Analyze the time complexity of the following code snippet.
import numpy as np
rng = np.random.default_rng()
random_numbers = rng.random(1000) # Generate 1000 random floats between 0 and 1
This code creates a random number generator and uses it to produce 1000 random numbers in one go.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Generating each random number involves internal calculations repeated for each number.
- How many times: The generation process repeats once for every number requested (here, 1000 times).
When you ask for more random numbers, the time to generate them grows roughly in direct proportion to how many you want.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 generation steps |
| 100 | About 100 generation steps |
| 1000 | About 1000 generation steps |
Pattern observation: Doubling the number of random numbers roughly doubles the work done.
Time Complexity: O(n)
This means the time to generate random numbers grows linearly with how many numbers you ask for.
[X] Wrong: "Generating 1000 random numbers takes the same time as generating 10 because it's just one function call."
[OK] Correct: Each random number requires its own calculation, so more numbers mean more work and more time.
Understanding how the time grows when generating random data helps you explain performance in data simulations and modeling tasks, a useful skill in many data science roles.
"What if we generate random numbers one by one in a loop instead of all at once? How would the time complexity change?"