0
0
NumPydata~10 mins

np.random.default_rng() modern approach in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - np.random.default_rng() modern approach
Create Generator with default_rng()
Use Generator methods (e.g., integers, random)
Get random numbers
Use numbers for analysis or simulation
End
Create a random number generator object, then use its methods to get random numbers for your tasks.
Execution Sample
NumPy
import numpy as np
rng = np.random.default_rng()
rands = rng.integers(1, 10, size=5)
print(rands)
This code creates a modern random number generator and prints 5 random integers between 1 and 9.
Execution Table
StepActionEvaluationResult
1Call np.random.default_rng()Create Generator objectrng = Generator instance
2Call rng.integers(1, 10, size=5)Generate 5 random integers from 1 to 9[7 2 5 1 8] (example output)
3Print randsOutput the array[7 2 5 1 8]
4EndNo more actionsExecution stops
💡 Finished generating and printing random integers using the modern Generator.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
rngNoneGenerator instanceGenerator instanceGenerator instanceGenerator instance
randsNoneNone[7 2 5 1 8][7 2 5 1 8][7 2 5 1 8]
Key Moments - 2 Insights
Why do we create 'rng' with default_rng() instead of using np.random.randint() directly?
Using default_rng() creates a Generator object that is more flexible and modern. The execution_table step 1 shows this creation, and step 2 uses its method integers() instead of the old global functions.
What does the 'size=5' argument do in rng.integers(1, 10, size=5)?
It tells the generator to produce 5 random numbers. Step 2 in the execution_table shows 5 numbers generated, matching the size argument.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what type of object is 'rng' after step 1?
AA list
BA Generator instance
CAn integer array
DNone
💡 Hint
Check the 'Result' column in step 1 of the execution_table.
At which step does the code generate the random integers?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Action' column describing rng.integers() in the execution_table.
If we change size=5 to size=3 in rng.integers(), how would the 'rands' variable change in variable_tracker?
AIt would become None
BIt would hold 5 random integers as before
CIt would hold 3 random integers instead of 5
DIt would cause an error
💡 Hint
Refer to the 'rands' row in variable_tracker and the meaning of the size argument in key_moments.
Concept Snapshot
np.random.default_rng() creates a modern random number generator.
Use its methods like integers() to get random numbers.
Specify size to get multiple numbers.
This approach is preferred over old np.random functions.
Example: rng = np.random.default_rng(); rng.integers(1,10,size=5)
Full Transcript
This visual execution shows how to use numpy's modern random number generator with np.random.default_rng(). First, we create a Generator object called rng. Then, we use rng.integers() to generate 5 random integers between 1 and 9. The execution table traces each step: creating the generator, generating numbers, printing them, and ending. The variable tracker shows how rng and the random numbers change during execution. Key moments clarify why we use default_rng() and what the size parameter does. The quiz tests understanding of the generator object, the step generating numbers, and the effect of changing size. The snapshot summarizes the modern approach syntax and usage.