Integer random with integers() 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 integers changes as we ask for more numbers.
How does the work grow when we increase the number of random integers generated?
Analyze the time complexity of the following code snippet.
import numpy as np
# Generate 1 million random integers between 0 and 9
random_numbers = np.random.default_rng().integers(low=0, high=10, size=1000000)
This code creates an array of one million random integers from 0 to 9.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Generating each random integer one by one internally.
- How many times: Once for each number requested (size of the array).
As we ask for more random numbers, the work grows directly with how many numbers we want.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 random numbers generated |
| 100 | About 100 random numbers generated |
| 1000 | About 1000 random numbers generated |
Pattern observation: The number of operations grows in a straight line with the input size.
Time Complexity: O(n)
This means the time to generate random integers grows directly with how many numbers you want.
[X] Wrong: "Generating many random numbers is almost instant no matter how many I ask for."
[OK] Correct: Each number requires some work, so asking for more numbers takes more time.
Understanding how time grows with input size helps you explain performance clearly and shows you know how algorithms scale in real tasks.
"What if we generate random integers without specifying the size (just one number)? How would the time complexity change?"
Practice
What does the numpy.random.integers(low, high) function do?
Solution
Step 1: Understand the function purpose
numpy.random.integers(low, high)generates random integers in a range.Step 2: Check the range behavior
The function generates fromlowinclusive tohighinclusive (defaultendpoint=True).Final Answer:
Generates random whole numbers from low (inclusive) to high (inclusive). -> Option BQuick Check:
integers() returns [low, high] random integers [OK]
- Thinking it generates decimal numbers
- Thinking high is exclusive
- Confusing with sequence generation
Which of the following is the correct syntax to generate 5 random integers between 1 and 10 using numpy.random.integers()?
import numpy as np # Your code here
Solution
Step 1: Check the function parameters
integers(low, high, size=number)generates an array of random integers.Step 2: Identify correct keyword for number of values
The correct keyword issize, sosize=5generates 5 numbers.Final Answer:
np.random.integers(1, 10, size=5) -> Option AQuick Check:
Use size= for array length [OK]
- Using positional third argument without size keyword
- Using wrong keyword like count or length
- Missing import statement
What is the output of the following code?
import numpy as np np.random.seed(0) arr = np.random.integers(1, 5, size=4) print(arr)
Solution
Step 1: Set random seed for reproducibility
Usingnp.random.seed(0)fixes the random numbers generated.Step 2: Generate 4 random integers between 1 and 5 inclusive
np.random.integers(1, 5, size=4)produces the array[4 4 1 3]with this seed.Final Answer:
[4 4 1 3] -> Option DQuick Check:
Seed 0 + integers(1,5,4) = [4 4 1 3] [OK]
- Ignoring seed and expecting different output
- Confusing inclusive range with exclusive
- Miscounting size parameter
Find the error in this code snippet and choose the correct fix:
import numpy as np arr = np.random.integers(0, 10, 5) print(arr)
Solution
Step 1: Check function signature
integers(low, high, size=...)acceptssizeas positional or keyword argument.Step 2: Identify argument usage
Passing 5 as positional third argument works correctly.Final Answer:
No error; code runs correctly as is. -> Option AQuick Check:
Positional size argument works [OK]
- Confusing integers() with randint()
- Using wrong keyword like count or length
- Assuming positional size causes error
You want to generate a 2x3 array of random integers from 10 to 20 using numpy.random.integers() and then filter to keep only even numbers. Which code correctly does this?
Solution
Step 1: Generate 2x3 array of integers 10 to 20
Usenp.random.integers(10, 20, size=(2,3))to get random integers in the range.Step 2: Filter only even numbers
Use boolean indexingarr[arr % 2 == 0]to keep even numbers only.Final Answer:
arr = np.random.integers(10, 20, size=(2,3)); arr = arr[arr % 2 == 0] -> Option CQuick Check:
Filter with modulo 2 equals zero for evens [OK]
- Multiplying random numbers instead of filtering
- Filtering odd numbers by mistake
- Using wrong range for even numbers
