randint() Function in Python
Last Updated :
30 Oct, 2023
randint() is an inbuilt function of the random module in Python3. The random module gives access to various useful functions one of them being able to generate random numbers, which is randint(). In this article, we will learn about randint in Python.
Python randint() Method Syntax
Syntax: randint(start, end)
Parameters :
(start, end) : Both of them must be integer type values.
Returns :
A random integer in range [start, end] including the end points.
Errors and Exceptions :
ValueError : Returns a ValueError when floating point values are passed as parameters.
TypeError : Returns a TypeError when anything other than numeric values are passed as parameters.
How randint() in Python work?
In this example, we are using the randint() method in Python to find a random number in a given range.
Python3
import random
r1 = random.randint( 0 , 10 )
print ( "Random number between 0 and 10 is % s" % (r1))
r2 = random.randint( - 10 , - 1 )
print ( "Random number between -10 and -1 is % d" % (r2))
r3 = random.randint( - 5 , 5 )
print ( "Random number between -5 and 5 is % d" % (r3))
|
Output
Random number between 0 and 10 is 2
Random number between -10 and -1 is -7
Random number between -5 and 5 is -3
The randint() Method Example
Multiple Randint Python Method Calls
In this example, we are making multiple random.randint() method calls in Python.
Python3
import random
beg,end = 1 , 1000
for i in range ( 5 ):
print (random.randint(beg, end))
|
Output
94
550
236
145
747
Program to Demonstrate the ValueError
In this example, we are seeing that if we passes the floating point values as parameters in the randint() function then a ValueError occurs.
Python3
import random
r1 = random.randint( 1.23 , 9.34 )
print (r1)
|
Output :
Traceback (most recent call last):
File "/home/f813370b9ea61dd5d55d7dadc8ed5171.py", line 6, in
r1=random.randint(1.23, 9.34)
File "/usr/lib/python3.5/random.py", line 218, in randint
return self.randrange(a, b+1)
File "/usr/lib/python3.5/random.py", line 182, in randrange
raise ValueError("non-integer arg 1 for randrange()")
ValueError: non-integer arg 1 for randrange()
Program to Demonstrate the TypeError
In this example, we can see that if we pass string or character literals as parameters in the randint() function then a TypeError occurs.
Python3
import random
r2 = random.randint( 'a' , 'z' )
print (r2)
|
Output :
Traceback (most recent call last):
File "/home/fb805b21fea0e29c6a65f62b99998953.py", line 5, in
r2=random.randint('a', 'z')
File "/usr/lib/python3.5/random.py", line 218, in randint
return self.randrange(a, b+1)
TypeError: Can't convert 'int' object to str implicitly
Applications : The randint() function can be used to simulate a lucky draw situation. Let’s say User has participated in a lucky draw competition. The user gets three chances to guess the number between 1 and 10. If guess is correct user wins, else loses the competition.
Python3
from random import randint
def generator():
return randint( 1 , 10 )
def rand_guess():
random_number = generator()
guess_left = 3
flag = 0
while guess_left > 0 :
guess = int ( input ( "Pick your number to "
"enter the lucky draw\n" ))
if guess = = random_number:
flag = 1
break
else :
print ( "Wrong Guess!!" )
guess_left - = 1
if flag is 1 :
return True
else :
return False
if __name__ = = '__main__' :
if rand_guess() is True :
print ( "Congrats!! You Win." )
else :
print ( "Sorry, You Lost!" )
|
Output
Pick your number to enter the lucky draw
8
Wrong Guess!!
Pick your number to enter the lucky draw
9
Wrong Guess!!
Pick your number to enter the lucky draw
0
Congrats!! You Win.
Share your thoughts in the comments
Please Login to comment...