0
0
NumPydata~10 mins

Integer random with integers() in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Integer random with integers()
Call np.random.Generator.integers(low, high, size)
Generate random integers in range [low, high))
Return array of random integers
Use or display the random integers
The function generates random integers between low (inclusive) and high (exclusive), then returns them as an array.
Execution Sample
NumPy
import numpy as np
rng = np.random.default_rng()
rnd_ints = rng.integers(1, 5, size=4)
print(rnd_ints)
This code creates a random number generator and generates 4 random integers between 1 and 4.
Execution Table
StepActionParametersGenerated IntegersOutput
1Create random generatorNoneN/Arng object created
2Call integers()low=1, high=5, size=4[3, 1, 4, 2]Array of 4 integers between 1 and 4
3Print outputArray from step 2[3 1 4 2][3 1 4 2] printed
4EndN/AN/AExecution stops
💡 All requested integers generated and printed; program ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
rngNoneGenerator objectGenerator objectGenerator objectGenerator object
rnd_intsNoneNone[3, 1, 4, 2][3, 1, 4, 2][3, 1, 4, 2]
Key Moments - 2 Insights
Why are the random integers always between 1 and 4, not including 5?
The integers() function generates numbers from low (inclusive) to high (exclusive). So 5 is not included, as shown in execution_table step 2.
What does the 'size' parameter control?
The 'size' parameter controls how many random integers are generated. In step 2, size=4 means 4 integers are returned.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the range of generated integers?
AFrom 1 to 4 inclusive
BFrom 0 to 4 inclusive
CFrom 1 to 5 inclusive
DFrom 1 to 3 inclusive
💡 Hint
Check the 'Parameters' and 'Generated Integers' columns in step 2.
According to variable_tracker, what is the value of rnd_ints after step 3?
ANone
B[3, 1, 4, 2]
CGenerator object
DAn empty array
💡 Hint
Look at the 'rnd_ints' row under 'After Step 3' in variable_tracker.
If we change size=2 in the code, how would execution_table step 2 change?
AGenerated Integers would have 4 numbers as before
BGenerated Integers would be empty
CGenerated Integers would have 2 numbers instead of 4
DThe code would error out
💡 Hint
The 'size' parameter controls how many integers are generated, see key_moments about 'size'.
Concept Snapshot
np.random.Generator.integers(low, high, size)
- Generates random integers from low (inclusive) to high (exclusive)
- 'size' sets how many integers to generate
- Returns a NumPy array of random integers
- Use default_rng() to create a generator
- Example: rng.integers(1, 5, size=4) -> 4 ints from 1 to 4
Full Transcript
This visual execution shows how to generate random integers using numpy's integers() method. First, a random number generator object is created with default_rng(). Then integers() is called with parameters low=1, high=5, and size=4. This produces 4 random integers between 1 and 4 inclusive. The generated array is stored in rnd_ints and printed. The execution table traces each step, showing the parameters and output. The variable tracker shows how rng and rnd_ints change over time. Key moments clarify why the range excludes the high value and what the size parameter does. The quiz tests understanding of the range, variable values, and effect of changing size. The snapshot summarizes usage in a quick reference format.