Quantcast
Viewing latest article 1
Browse Latest Browse All 10

Create random number in Python

In this Python tutorial, we will study about another Python concept of creating 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 generates a 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

 

 

The post Create random number in Python appeared first on ByteArray.

Viewing latest article 1
Browse Latest Browse All 10

Trending Articles