Create random number in Python

Do you want to know how to create random number in Python, here we will discuss about it.

In Python, Random number is generated using random function.  First we need to import random module.

import random

Now we use randint() function of random.  randint() function takes two arguments.Let’s see example to understand better.

#Generate random number in python

import random

num= random.randint(1,100)

print(num) 

 

8

6

100

35

Above example shows how to create random number in Python.

  • randint function takes two arguments 1 and 100, it means random number will generate from 1 to 100. we can change the range of random numbers to be generate by changing these arguments.

 

There is another function random() which takes no arguments , but it generate random number between o and 1. see below

 

#Generate random number in python

import random

num= random.random()

print(num) 

When we run program with random() function as discussed above then we get output like this

 

0.502516067453484

0.5987878032362247

 

 

Please follow and like us:
READ  Python for loop example