0
0
NumPydata~5 mins

Normal distribution with normal() in NumPy - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Normal distribution with normal()
O(n)
Understanding Time 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?

Scenario Under Consideration

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.

Identify Repeating Operations

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).
How Execution Grows With Input

When you ask for more numbers, the work grows in a straight line with the amount you want.

Input Size (n)Approx. Operations
1010 random numbers generated
100100 random numbers generated
10001000 random numbers generated

Pattern observation: Doubling the input size doubles the work needed.

Final Time Complexity

Time Complexity: O(n)

This means the time to generate numbers grows directly in proportion to how many numbers you want.

Common Mistake

[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.

Interview Connect

Understanding how generating random data scales helps you reason about performance in simulations and data analysis tasks.

Self-Check

"What if we generate a 2D array of random numbers instead of 1D? How would the time complexity change?"