Uniform random with random() 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 it takes to generate random numbers grows as we ask for more numbers.
How does the cost change when we increase the amount of random values generated?
Analyze the time complexity of the following code snippet.
import numpy as np
n = 1000
random_numbers = np.random.random(n)
This code generates n random numbers between 0 and 1 using NumPy's random() function.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Generating each random number independently.
- How many times: Exactly
ntimes, once per number requested.
Each random number takes about the same time to generate, so the total time grows directly with how many numbers we ask for.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 random generations |
| 100 | 100 random generations |
| 1000 | 1000 random generations |
Pattern observation: Doubling n roughly doubles the work needed.
Time Complexity: O(n)
This means the time to generate random numbers grows in a straight line with the number of values requested.
[X] Wrong: "Generating multiple random numbers is done all at once, so time stays the same no matter how many numbers we ask for."
[OK] Correct: Each number requires its own calculation, so more numbers mean more work and more time.
Understanding how generating random data scales helps you reason about performance in simulations and data sampling tasks.
"What if we generate a 2D array of random numbers with shape (n, m)? How would the time complexity change?"
Practice
What does numpy.random.random() generate by default?
Solution
Step 1: Understand the function purpose
numpy.random.random()generates random floats in the range [0.0, 1.0).Step 2: Check default behavior
Without any size argument, it returns a single float number between 0 and 1.Final Answer:
A single random number between 0 and 1 -> Option AQuick Check:
random() = single float [OK]
- Thinking it returns integers
- Assuming range is -1 to 1
- Expecting an array without size
Which of the following is the correct syntax to generate a 2x3 array of uniform random numbers using numpy.random.random()?
Solution
Step 1: Recall correct function call
Thesizeparameter expects a tuple for shape, so use parentheses and commas inside.Step 2: Check syntax options
Onlynumpy.random.random((2,3))correctly passes a tuple for size.Final Answer:
numpy.random.random((2,3)) -> Option AQuick Check:
Tuple size = (2,3) [OK]
- Using square brackets instead of parentheses
- Passing size as separate arguments
- Using curly braces instead of parentheses
What is the output shape of the following code?
import numpy as np arr = np.random.random(5)
Solution
Step 1: Understand the size parameter
Passing 5 as size creates a 1D array with 5 elements.Step 2: Determine shape of the array
A 1D array with 5 elements has shape (5,), not (1,5) or (5,1).Final Answer:
(5,) -> Option CQuick Check:
random(5) shape = (5,) [OK]
- Confusing 1D shape with 2D shapes
- Expecting a scalar output
- Assuming shape is (1,5) or (5,1)
Identify the error in this code snippet:
import numpy as np arr = np.random.random[3,4]
Solution
Step 1: Check function call syntax
Functions in Python require parentheses, not square brackets.Step 2: Identify the error
Using square brackets[]causes a syntax error; correct israndom((3,4)).Final Answer:
Using square brackets instead of parentheses for function call -> Option DQuick Check:
Function calls need parentheses [OK]
- Using square brackets for function calls
- Confusing size argument type
- Assuming random() can't take size
You want to create a 3x3 matrix of uniform random numbers but only want numbers greater than 0.5. Which code correctly achieves this?
Solution
Step 1: Understand the goal
Create a 3x3 matrix where all uniform random numbers are greater than 0.5.Step 2: Analyze each option
A indexes one random array with a mask from another, yielding a 1D array with values in [0,1) including some <0.5. B returns a 3x3 boolean array. C produces values in [0,0.5]. D shifts values to [0.5,1.5), ensuring all ≥0.5.Final Answer:
np.random.random((3,3)) + 0.5 -> Option BQuick Check:
+0.5 shifts to [0.5, 1.5) all >0.5 [OK]
- Boolean indexing with different array (A: values can be <0.5, shape changes)
- Comparison returns booleans (B)
- Scaling down makes values ≤0.5 (C)
