Using np.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 D
Quick Check:
Seed 0 + integers(1,5,4) = [4 4 1 3] [OK]
Hint: Use seed to get repeatable random integers [OK]
Common Mistakes:
Ignoring seed and expecting different output
Confusing inclusive range with exclusive
Miscounting size parameter
4.
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)
medium
A. No error; code runs correctly as is.
B. Change third argument to size=5 to specify array size.
C. Add parentheses around arguments: integers((0,10),5).
D. Replace integers with randint to fix syntax.
Solution
Step 1: Check function signature
integers(low, high, size=...) accepts size as 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 A
Quick Check:
Positional size argument works [OK]
Hint: size can be positional or keyword in integers() [OK]
Common Mistakes:
Confusing integers() with randint()
Using wrong keyword like count or length
Assuming positional size causes error
5.
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?
hard
A. arr = np.random.integers(5, 10, size=(2,3)) * 2