Uniform distribution in SciPy - Time & Space Complexity
We want to understand how the time to generate uniform random numbers grows as we ask for more numbers.
How does the cost change when we increase the amount of data we create?
Analyze the time complexity of the following code snippet.
import numpy as np
from scipy.stats import uniform
# Generate 1D array of n uniform random numbers between 0 and 1
n = 1000
samples = uniform.rvs(size=n)
This code generates n random numbers from a uniform distribution between 0 and 1.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Generating each random number independently.
- How many times: Exactly
ntimes, once per sample.
Each additional number requires one more generation step, so the total work grows directly with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 random number generations |
| 100 | 100 random number generations |
| 1000 | 1000 random number generations |
Pattern observation: Doubling n doubles the work needed.
Time Complexity: O(n)
This means the time to generate samples grows in a straight line with the number of samples requested.
[X] Wrong: "Generating multiple uniform samples is constant time because it's just one function call."
[OK] Correct: Each sample requires work, so more samples mean more time, not the same time.
Understanding how data generation scales helps you explain performance when working with simulations or sampling in real projects.
"What if we generated a 2D array of samples instead of 1D? How would the time complexity change?"