# How to generate random number in Python

Python has its own module to generate a random number. The module is the** random **module. By using this special module you can easily generate random number in Python.

In this tutorial, we will learn how to generate a random number in Python using the random module.

You may also read,

## Generate random number using random module

In order to generate a random number in Python we will use the `randint()`

function from the random module.

In Random module we have plenty of functions available for us but in this tutorial, we will only use `randint()`

to generate a random number in a certain range.

Below is the syntax of this function:

random.randint(a,b)

As you can see this function has two parameters and both of those are mandatory or you can say the required parameter.

`a`

is the lowest range and `b`

is the highest range here. The number will be generated within this lowest and highest range.

To give you a better understanding below is a simple example of using the `randint()`

function to generate a randomized number within the range between 3 and 20:

# generate a random number in a range of 5 and 15 import random print(random.randint(3,20))

Output: Anything from 3 to 20.

In the above program, 3 is the lowest, and 20 is the highest range.

By using this function you can generate a random number for a given range in Python

### Generate a random number that is divisible by n

If you wish to generate a random number that must be divisible by a particular number then you can use the `randint()`

function in such a way as you can see below:

import random print(random.randint(1,10)*5)

Output: Always print a number that is divisible by 5

The maximum range of the random number will be 10*5=50

The minimum value of the random number will be 1*5=5

so the basic code will be like this:

import random print(random.randint(a,b)*n)

Where the random number will be divisible by n.

The range of the random number will be from a*n to b*n

How to detect strings that contain only whitespaces in Python

## Leave a Reply