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
Using np.random.default_rng() to Generate Random Numbers
📖 Scenario: Imagine you are a data scientist who needs to generate random numbers for a simulation. Using the modern np.random.default_rng() method helps you create random numbers in a simple and reliable way.
🎯 Goal: You will create a random number generator, set a seed for reproducibility, generate a list of random integers, and then print the list.
📋 What You'll Learn
Create a random number generator using np.random.default_rng()
Set the seed to 123 for reproducibility
Generate 5 random integers between 10 and 50 (inclusive)
Print the list of generated random integers
💡 Why This Matters
🌍 Real World
Random number generation is used in simulations, games, and testing to create unpredictable but reproducible data.
💼 Career
Data scientists and analysts use random number generators to create sample data, run simulations, and test models.
Progress0 / 4 steps
1
Create a random number generator
Import numpy as np and create a random number generator called rng using np.random.default_rng().
NumPy
Hint
Use rng = np.random.default_rng() to create the generator.
2
Set the seed for reproducibility
Create a random number generator called rng with the seed 123 using np.random.default_rng(123).
NumPy
Hint
Pass the seed 123 as an argument to default_rng().
3
Generate 5 random integers between 10 and 50
Use the rng.integers() method to generate 5 random integers between 10 (inclusive) and 51 (exclusive) and store them in a variable called random_numbers.
NumPy
Hint
Use rng.integers(10, 51, size=5) to get 5 integers from 10 to 50.
4
Print the list of random integers
Print the variable random_numbers to display the generated random integers.
NumPy
Hint
Use print(random_numbers) to show the numbers.
Practice
(1/5)
1. What does np.random.default_rng() do in NumPy?
easy
A. Creates a modern random number generator instance
B. Generates a fixed list of numbers
C. Imports the NumPy library
D. Sorts an array in ascending order
Solution
Step 1: Understand the function purpose
np.random.default_rng() creates a new random number generator object using the modern Generator API.
Step 2: Compare with other options
It does not generate fixed lists, import libraries, or sort arrays.
Final Answer:
Creates a modern random number generator instance -> Option A
Quick Check:
default_rng() = modern RNG instance [OK]
Hint: Remember default_rng() always creates a new RNG object [OK]
Common Mistakes:
Confusing it with random number generation output
Thinking it imports NumPy
Mixing it up with sorting functions
2. Which of the following is the correct way to create a random number generator with a seed of 42 using np.random.default_rng()?
easy
A. rng = np.random.default_rng(42, seed=42)
B. rng = np.random.default_rng(42)
C. rng = np.random.default_rng().seed(42)
D. rng = np.random.default_rng().random(42)
Solution
Step 1: Check the correct syntax for seeding
The seed is passed as an argument directly to default_rng(), so np.random.default_rng(42) is correct.
Step 2: Evaluate other options
rng = np.random.default_rng(42, seed=42) is invalid because it passes seed both positionally and as keyword, causing TypeError: multiple values for 'seed'. rng = np.random.default_rng().seed(42) tries to call seed() method which does not exist on the Generator. rng = np.random.default_rng().random(42) calls random(42) which generates numbers, not seeds.
Final Answer:
rng = np.random.default_rng(42) -> Option B
Quick Check:
Seed passed as argument = rng = np.random.default_rng(42) [OK]
The random() method of the Generator does not accept a seed parameter; seeding is done when creating the generator.
Step 2: Identify the error
Passing seed=10 to random() causes a TypeError.
Final Answer:
random() does not accept a seed argument -> Option C
Quick Check:
Seed only in default_rng(), not in random() [OK]
Hint: Seed only when creating RNG, not in random() calls [OK]
Common Mistakes:
Trying to seed random() method
Thinking random() returns integers
Believing default_rng() needs seed always
5. You want to generate a reproducible shuffled version of the list [10, 20, 30, 40, 50] using np.random.default_rng(). Which code correctly achieves this?
To get reproducible shuffling, seed the generator and use its methods. rng.shuffle() shuffles in-place and returns None, while rng.permutation() returns a shuffled copy.
Step 2: Analyze options
rng = np.random.default_rng(7)
arr = [10, 20, 30, 40, 50]
rng.shuffle(arr)
print(arr) fails because rng.shuffle() requires a NumPy ndarray but arr is a list (TypeError). rng = np.random.default_rng()
arr = [10, 20, 30, 40, 50]
rng.shuffle(arr)
print(arr) has no seed, so not reproducible (also fails on list). rng = np.random.default_rng(7)
arr = [10, 20, 30, 40, 50]
shuffled = np.random.shuffle(arr)
print(shuffled) incorrectly uses np.random.shuffle() which ignores rng, requires ndarray (fails on list), and returns None. rng = np.random.default_rng(7)
arr = [10, 20, 30, 40, 50]
shuffled = rng.permutation(arr)
print(shuffled) seeds and uses permutation() to get a reproducible shuffled copy.