np.random.randint()
randint draws random whole numbers from a range you specify.
In this page:
Syntax
np.random.randint(low, high, size)
np.random.randint()
np.random.randint(low, high, size) returns integers from low up to but not including high. If you give one argument, it means high and low defaults to 0. Use size to build arrays of any shape.
Note:
The high value is exclusive, so randint(1, 7) simulates a die.
Example: np.random.randint()
import numpy as np
np.random.seed(3)
print(np.random.randint(1, 7, size=10))
print(np.random.randint(0, 100, size=(2, 3)))
# Output:
# [3 1 2 4 1 1 1 6 6 4]
# [[74 41 10]
# [21 38 96]]
Related Topics
Common Mistakes
- Expecting high to be included
- Forgetting size returns a single number
- Using rand and casting to int
Chapter Summary
- high is exclusive
- size controls the output shape
- One argument means high
- Returns integers
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: