# 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. But the random module can do more than that. random module in Python can generate a random number in Python as well as it can be used to pick a random element from a list. It can also be used to shuffle item easily. In a word you can say that random module is very much useful to do such things involved with the term random.

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

You may also read,

## Generate random number in Python using random module

In order to generate a random number in Python we will use randint() function.

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.

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.

To give you a better understand I can say that if you wish to create a random number in Python which must be in a range of 5 to 15. Then you have to do that like the below:

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

Output: Anything from 5 to 15

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

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

If you wish to generate a random number which must be divisible by a particular number then you can use this:

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

Output: Always print a number which 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

You must be logged in to post a comment.