# How to generate a random integer using python ?

Published: January 01, 2024

Updated: January 01, 2024

Tags: Python; Random;

## Introduction

Randomness is an essential aspect of any programming language, as it allows programs to generate different outputs each time they run. In Python, the `random` module enables us to work with random numbers and perform various operations on them. However, if we need a more sophisticated approach for generating random integers, we can use the numpy library.

## Using Python Random randint() Method

To generate random integers in Python, we can use the randint() method from the random module. This module is part of the standard library in Python, which means that it comes pre-installed with any Python installation.

However, we need to first import the module before we can use it in our code.

To import the `random` module, we can use the following line of code:

````import random`
```

Once the module is imported, we can call the randint() method. The randint() method takes in two parameters, `a` and `b`, which represent the lower and upper bounds of our random integer.

For example, if we want to generate a random integer between 0 and 10 [0,10] (please note that the range includes both 0 and 10), we would use the following code:

````import random`

`random.randint(0,10)`
```

The output of this code will be a random integer between 0 and 10, inclusive of both bounds. Keep in mind that each time we run this code, we will get a different value.

### Create a list of random integers

For instance, a list comprehension can be employed to generate a list of random integers:

````[random.randint(0,10) for i in range(20)]`
```

the above code will generate for example the following result:

````[6, 10, 5, 3, 0, 1, 10, 9, 4, 0, 0, 2, 10, 9, 7, 6, 9, 3, 3, 8]`
```

### Generate always the same random numbers

To consistently generate the same random number, we can utilize the function random.seed(). This function takes in an integer as the seed value and sets it as the starting point for generating random numbers, For example

````random.seed(42)`

`[random.randint(0,10) for i in range(20)]`
```

will generate the same random numbers:

````[10, 1, 0, 4, 3, 3, 2, 1, 10, 8, 1, 9, 6, 0, 0, 1, 3, 3, 8, 9]`
```

This seed() function can be useful for code testing purposes or for educational endeavors.

## Using Numpy's randint() Method

Apart from the random module, we can also use the randint() method from the numpy library. Numpy is a powerful numerical computing library that offers a wide range of functions for handling large arrays and matrices

### Create an array of random integers

However, this time we will use numpy's randint() method instead of the one from the random module.

In this case, the randint() method takes in three parameters, `low`, `high`, and `size`. The first two parameters represent the lower and upper bounds of our random integer, while the third parameter (optional) specifies the shape or size of our output. If we do not specify the `size` parameter, numpy will return a single random integer. However, if we pass in a tuple specifying the shape, numpy will return an array of random integers with the specified shape. For example, if we want to generate am array of 10 random integers:

````import numpy as np`

`data = np.random.randint(0,10, size=10)`

`print(data)`
```

will generate for example

````array([8, 1, 0, 0, 4, 4, 3, 6, 1, 3])`
```

Another example, if we want to generate a 3x3 matrix of random integers between -5 and 5 (exclusive), we would use the following code:

````np.random.randint(-5, 5, size=(3,3))`
```

The output of this code will be a 3x3 array of random integers between -5 and 5

````array([[ 4,  3, -2],`
`       [-1, -3, -4],`
`       [-2,  0, -2]])`
```

### Difference with the randint function in the random Python module

It is important to note the key distinction between the randint function in the random Python module, which generates random numbers within the range [a, b], and the numpy randint function, which generates random numbers within the range [a, b[ (excluding b).

### Generate always the same random numbers

To consistently generate the same random number, we can utilize the numpy function random.seed(). This function takes in an integer as the seed value and sets it as the starting point for generating random numbers, For example

````np.random.seed(42)`

`np.random.randint(-5, 5, size=(3,3))`
```

will generate the same random numbers:

````array([[ 1, -2,  2],`
`       [-1,  1,  4],`
`       [-3,  1,  2]])`
```