We use np.random.default_rng() to create a modern random number generator. It helps us get random numbers in a simple and reliable way.
np.random.default_rng() modern approach in NumPy
Start learning this pattern below
Jump into concepts and practice - no test required
rng = np.random.default_rng(seed=None)seed is optional. If you give a number, you get the same random numbers every time.
This method is recommended over older np.random functions for better randomness and features.
import numpy as np rng = np.random.default_rng() print(rng.integers(1, 10))
rng = np.random.default_rng(seed=42) print(rng.random())
rng = np.random.default_rng() sample = rng.choice([10, 20, 30, 40], size=2, replace=False) print(sample)
This program shows how to create a modern random number generator with a seed. It generates random integers, random floats, and shuffles a list.
import numpy as np # Create a random number generator with a fixed seed rng = np.random.default_rng(seed=123) # Generate 5 random integers between 0 and 99 random_integers = rng.integers(0, 100, size=5) # Generate 3 random floats between 0 and 1 random_floats = rng.random(3) # Randomly shuffle a list data = [1, 2, 3, 4, 5] rng.shuffle(data) print("Random integers:", random_integers) print("Random floats:", random_floats) print("Shuffled list:", data)
Using a seed makes your random results repeatable, which is useful for debugging.
The default_rng() method is faster and more flexible than older random functions.
Always use default_rng() for new projects instead of np.random.seed() or np.random.rand().
np.random.default_rng() creates a modern random number generator.
It supports many random operations like integers, floats, shuffling, and sampling.
Using a seed gives you the same random numbers every time you run the code.
Practice
np.random.default_rng() do in NumPy?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 AQuick Check:
default_rng() = modern RNG instance [OK]
- Confusing it with random number generation output
- Thinking it imports NumPy
- Mixing it up with sorting functions
np.random.default_rng()?Solution
Step 1: Check the correct syntax for seeding
The seed is passed as an argument directly todefault_rng(), sonp.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 callseed()method which does not exist on the Generator. rng = np.random.default_rng().random(42) callsrandom(42)which generates numbers, not seeds.Final Answer:
rng = np.random.default_rng(42) -> Option BQuick Check:
Seed passed as argument = rng = np.random.default_rng(42) [OK]
- Passing seed both positionally and as keyword argument
- Calling seed() method on the generator
- Confusing random() method with seeding
import numpy as np rng = np.random.default_rng(123) print(rng.integers(1, 10, size=3))
Solution
Step 1: Understand the code
The code creates a random number generator with seed 123, then generates 3 random integers between 1 (inclusive) and 10 (exclusive).Step 2: Run the code or recall output
Running this code produces the array[3 3 7].Final Answer:
[3 3 7] -> Option AQuick Check:
Seed 123 + integers(1,10,3) = [3 3 7] [OK]
- Assuming inclusive upper bound 10
- Confusing seed effect on output
- Mixing output with floats instead of integers
import numpy as np rng = np.random.default_rng() random_numbers = rng.random(5, seed=10) print(random_numbers)
Solution
Step 1: Check method signature of random()
Therandom()method of the Generator does not accept aseedparameter; seeding is done when creating the generator.Step 2: Identify the error
Passingseed=10torandom()causes a TypeError.Final Answer:
random() does not accept a seed argument -> Option CQuick Check:
Seed only in default_rng(), not in random() [OK]
- Trying to seed random() method
- Thinking random() returns integers
- Believing default_rng() needs seed always
[10, 20, 30, 40, 50] using np.random.default_rng(). Which code correctly achieves this?Solution
Step 1: Understand reproducible shuffling
To get reproducible shuffling, seed the generator and use its methods.rng.shuffle()shuffles in-place and returns None, whilerng.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 becauserng.shuffle()requires a NumPy ndarray butarris 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 usesnp.random.shuffle()which ignoresrng, 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 usespermutation()to get a reproducible shuffled copy.Final Answer:
rng = np.random.default_rng(7) arr = [10, 20, 30, 40, 50] shuffled = rng.permutation(arr) print(shuffled) -> Option DQuick Check:
Seed + permutation() = reproducible shuffled copy [OK]
- Using np.random.shuffle() ignoring seed
- Expecting shuffle() to return a new list
- Not seeding the generator for reproducibility
