Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Integer Random Numbers with numpy.integers()
📖 Scenario: You are working on a simple data science task where you need to generate random integer data for analysis. This is common when simulating data or testing algorithms.
🎯 Goal: Learn how to use numpy.random.Generator.integers() to create random integers within a specific range.
📋 What You'll Learn
Create a numpy random Generator instance
Generate random integers between 10 and 20
Generate exactly 5 random integers
Print the generated random integers
💡 Why This Matters
🌍 Real World
Generating random integers is useful in simulations, testing algorithms, and creating sample datasets.
💼 Career
Data scientists often need to create random data for experiments and model testing.
Progress0 / 4 steps
1
Create a numpy random Generator instance
Import numpy as np and create a random number generator instance called rng using np.random.default_rng().
NumPy
Hint
Use np.random.default_rng() to create the generator.
2
Set the range for random integers
Create two variables called low and high and set them to 10 and 20 respectively. These will define the range for the random integers.
NumPy
Hint
Assign 10 to low and 20 to high.
3
Generate 5 random integers between low and high
Use the integers() method of rng to generate 5 random integers between low (inclusive) and high (exclusive). Store the result in a variable called random_numbers.
NumPy
Hint
Use rng.integers(low, high, size=5) to generate the numbers.
4
Print the generated random integers
Print the variable random_numbers to display the generated random integers.
NumPy
Hint
Use print(random_numbers) to show the array.
Practice
(1/5)
1.
What does the numpy.random.integers(low, high) function do?
easy
A. Generates random numbers only equal to low or high.
B. Generates random whole numbers from low (inclusive) to high (inclusive).
C. Generates a sequence of numbers from low to high.
D. Generates random decimal numbers between low and high.
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 from low inclusive to high inclusive (default endpoint=True).
Final Answer:
Generates random whole numbers from low (inclusive) to high (inclusive). -> Option B
Quick Check:
integers() returns [low, high] random integers [OK]
Hint: integers() generates [low, high] by default [OK]
Common Mistakes:
Thinking it generates decimal numbers
Thinking high is exclusive
Confusing with sequence generation
2.
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
easy
A. np.random.integers(1, 10, size=5)
B. np.random.integers(1, 10, count=5)
C. np.random.integers(low=1, high=10, count=5)
D. np.random.integers(1, 10, length=5)
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 is size, so size=5 generates 5 numbers.
Final Answer:
np.random.integers(1, 10, size=5) -> Option A
Quick Check:
Use size= for array length [OK]
Hint: Use size= to specify number of random integers [OK]
Common Mistakes:
Using positional third argument without size keyword
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