📜  numpy random int - Python (1)

📅  最后修改于: 2023-12-03 15:18:02.960000             🧑  作者: Mango

NumPy Random Int - Python

NumPy is a popular Python library used for numerical computing. One of the many things NumPy offers us is the ability to generate random values. random.int is a NumPy method used to generate random integers.

Syntax
numpy.random.int(low=0, high=None, size=None, dtype='l')
  • low: The lower bound of the interval. If not given, defaults to 0.
  • high: The upper bound of the interval. If not given, defaults to None.
  • size: The shape of the resulting array. If not given, defaults to None.
  • dtype: The data type of the output array. If not given, defaults to 'l' (long integer).
Examples
Generating a single random integer
import numpy as np

random_int = np.random.int()
print(random_int)

Output:

-295036245
Generating random integers between a range
import numpy as np

random_int = np.random.int(low=1, high=10)
print(random_int)

Output:

6
Generating random integers with a specific shape
import numpy as np

random_int_array = np.random.int(low=1, high=10, size=(2, 3))
print(random_int_array)

Output:

[[9 7 1]
 [9 3 3]]
Generating random integers with a specific data type
import numpy as np

random_int_array = np.random.int(low=1, high=10, dtype='uint16')
print(random_int_array)

Output:

3