How to use randint() Function in Python

In this tutorial, we will discuss how to use randint() function in Python.

randint() is an inbuilt function of the random module. By importing random module we can use all of it functions.

randint() will be able to generate random numbers in the given range.

randint() function in Python

Syntax:

randint(start,end)

Note that start and end should be integer values.

If float values are given in parameters then it will throw a ValueError

If other than numeric values are assigned to start and end then it will throw a TypeError

Let’s see the code

#import random module
import random

#Generating a random value between 0 and 10 using randint()
r=random.randint(0,10)
print("Random number between 0 and 10 is",r)

#Generating a random value between -10 and -1 using randint()
p=random.randint(-10,-1)
print("Random numer between -10 and -1 is",p)

Output:

Random number between 0 and 10 is 4
Random number between -10 and -1 is -5

Also read:

Leave a Reply

Your email address will not be published. Required fields are marked *