# How to Generate Random Integers in Range with Numpy

Numpy is a Python library that provides a comprehensive mathematical library. It includes functions for generating random numbers, among other things.

Let’s learn how to generate random integers in range with Numpy. We will use Numpy randint method for that purpose.

## Np random randint

The most common way to generate random integers in range with Numpy is to use the randint() method. The syntax for the randint() method is as follows:

```import numpy as np

np.random.randint(start, stop, size)
```

The start parameter specifies the starting value of the range. The stop parameter specifies the ending value of the range. The size parameter specifies the number of random integers to generate.

For example, the following code will generate 5 random integers between 0 and 10:

```import numpy as np

random_integers = np.random.randint(0, 10, 5)

print(random_integers)
```

The output shows that the 5 random integers generated are 2, 7, 9, 3, and 4.

To generate random integers just use randint and reshape Numpy methods. The randint syntax is as in below example.

```import numpy as np

my_array = np.random.randint(0, 10, 50).reshape(5, -1)
print(f"My array: \n {my_array}")

```

np.random.randint(0, 10, 50) generates random integers between 0 and 10. There are 50 of them.

## Other Ways to Generate Random Integers in Range with Numpy

In addition to the randint() method, there are a few other ways to generate random integers in range with Numpy.

• The random.choice() method can be used to choose a random element from an array.
• The random.sample() method can be used to randomly sample a number of elements from an array.
• The random.shuffle() method can be used to shuffle the elements of an array.
See also  How to convert array from float to int in Numpy?