Normal distribution with normal() in NumPy - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
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?"
Practice
loc parameter control in the numpy.random.normal() function?Solution
Step 1: Understand the parameters of normal()
Thenormal()function has parameterslocandscale.locsets the mean (center) of the distribution.Step 2: Identify the role of
The mean is the center point where most values cluster in a bell curve.locFinal Answer:
The center (mean) of the normal distribution -> Option BQuick Check:
loc= center [OK]
- Confusing loc with scale
- Thinking loc controls number of samples
- Assuming loc changes distribution shape
Solution
Step 1: Recall the correct function and parameters
The function isnumpy.random.normal()with parameterslocfor mean,scalefor std dev, andsizefor number of samples.Step 2: Match parameters to correct syntax
numpy.random.normal(loc=10, scale=2, size=5) correctly usesloc=10,scale=2, andsize=5.Final Answer:
numpy.random.normal(loc=10, scale=2, size=5) -> Option DQuick Check:
Correct parameter names and order [OK]
- Using wrong parameter names like mean or std
- Mixing order without keywords
- Calling numpy.normal instead of numpy.random.normal
import numpy as np arr = np.random.normal(loc=0, scale=1, size=(3,4)) print(arr.shape)
Solution
Step 1: Understand the size parameter
Thesizeargument is set to(3,4), which means generate a 2D array with 3 rows and 4 columns.Step 2: Check the shape of the generated array
Printingarr.shapereturns the shape tuple, which matches the size argument.Final Answer:
(3, 4) -> Option CQuick Check:
size=(3,4) means shape=(3,4) [OK]
- Confusing rows and columns order
- Expecting flattened array shape
- Ignoring tuple format for size
import numpy as np samples = np.random.normal(mean=0, std=1, size=10) print(samples)
Solution
Step 1: Check parameter names for normal()
The functionnp.random.normal()expectslocfor mean andscalefor standard deviation, notmeanorstd.Step 2: Verify other parts of the code
Import is correct, size can be integer, and print syntax is valid.Final Answer:
Incorrect parameter names: should use loc and scale instead of mean and std -> Option AQuick Check:
Use loc and scale for mean and std [OK]
- Using mean or std instead of loc and scale
- Thinking size must be tuple always
- Assuming print syntax error
Solution
Step 1: Generate temperatures with correct parameters
Useloc=20for mean temperature andscale=3for standard deviation, withsize=7for a week.Step 2: Calculate the average temperature correctly
Usetemps.mean()to get the average. Round for neat output.Final Answer:
temps = np.random.normal(loc=20, scale=3, size=7) avg_temp = temps.mean() print(round(avg_temp, 2)) -> Option AQuick Check:
loc=mean, scale=std, mean() for average [OK]
- Swapping loc and scale values
- Using mean or std instead of loc and scale
- Using sum() instead of mean() for average
- Using median() instead of mean()
