The random module in Python allows you to generate pseudo-random variables. The module provides various methods to get the random variables, one of which is the randint method. The randint Python function is a built-in method that lets you generate random integers using the random module.

Syntax of the Randint Python Function

random.randint(start, end)

Parameters Used in Randint Python Function

As you can see in the syntax, the Python randint() function accepts two parameters, which are:

  • start: It is a required parameter that accepts integer value and determines the starting range from which it will generate the random integer.
  • end: It is a required parameter that accepts integer value and defines the ending of the range from which it will generate the random integer.

Return Value of the Python randint() Function

The randint Python function returns a randomly generated integer from the specified range, with both inclusive parameters.

Example: Demonstrating the Use of Randint Python Function

# importing the random module

import random

# using the randint function

int1 = random.randint(0, 5)

print("Random number generated between 0 and 5 using the randint() function is % s" % (int1))



Here, you need to first import the random module in the above code, and then use the randint Python function to generate a random integer from 0 to 5.

Note: Your output may vary as a random integer is selected and returned by the randint() function.

Python Training Course

Learn Data Operations in PythonExplore Course
Python Training Course

What Errors and Exceptions Can Occur While Using Randint in Python?

The Python randint() function can throw the following two errors:

  • ValueError: This error is thrown if you try to pass floating-point numbers as arguments
  • TypeError: This error is thrown if you try to pass anything except numeric values as arguments

Example: Getting ValueError for Randint Python Function

In the code below, you will try passing a floating-point value as a parameter to get the ValueError while compiling the program.

# importing the random module

import random

# using the randint function

int1 = random.randint(0.0, 4.9)




As you passed a floating-point value as the second (end) parameter, you got a ValueError while running the code.

Example: Getting TypeError for Randint Python Function

In the code below, you will try passing a string value as a parameter to get the TypeError while compiling the program intentionally.

# importing the random module

import random

# using the randint function

int1 = random.randint('x', 'y')




As you can see in the output, this demo got a TypeError saying ‘can only concatenate str (not “int”) to str’ as you passed string values instead of integers.

Free Course: Programming with Python

Learn the Basics of Programming with PythonEnroll Now
Free Course: Programming with Python

What are the Applications of Randint Python Function?

Since the Python randint() function generates a pseudo-random integer, it is usually helpful in gaming and lottery applications. For instance, let’s say a participant gets three chances to guess the following random number generated within a range. If the person gets it correct within the three attempts, he wins, or else loses. Let’s simulate this situation using the randint Python function in the example below.

Example: Creating a Lucky Draw Game with the randint() Function

In the below code, you will generate a random number between 1 and 20 three times. You will then accept input from the user and check if the guess is correct and wins. Let’s begin with our example.

# importing randint function

from random import randint

# Function to generate new random integer

def random_generator():

return randint(1, 20)

# Function to take input from user and show results

def your_guess():

# calling function to generate random numbers

rand_number = random_generator()

# defining the number of

# guesses the user gets

remaining_gus = 3

# Setting win-condition checker flagship variable

flagship = 0

# Using loop to provide only three chances

while remaining_gus > 0:

# Taking user input

guess_num = int(input("What's your guess?\n"))

# checking if the guess is correct

if guess_num == rand_number

# setting flagship 1 for correct guess to

# break loop

flagship = 1



# printing the failure message

print("Oops, you missed!")

# Decrementing guesses left

remaining_gus -= 1

# your_guess function returns True if win-condition

# is satisfied

if flagship == 1:

return True

# else returns False


return False

# Final output code to decide win or lose

if __name__ == '__main__':

if your_guess() is True:

print("Wow, you hit the bull's eye!")

else :

print("Sorry, better luck next time!")



Second attempt output:


Well, as it is evident, you could not get it correct in the first game. But in the second game, you hit the bull’s eye in the very first attempt. Copy-paste the code in any Python compiler, and you too can enjoy the game. You can compete with your friends and family members, and make a day out of it.

What Happens in Case of Multiple randint() Method Call?

You have seen that when you call the randint Python function; it generates and returns a random integer from the specified range. But what if you call it multiple times? Can it return the same value twice? If not, what happens if the number of calls is more than the available range of values? Let’s get all these queries answered.

FREE Data Science With Python Course

Start Learning Data Science with Python for FREEStart Learning
FREE Data Science With Python Course

Example: Calling the Randint Python Function Multiple Times

In this example, you will keep the available range larger. Let’s see the output.

import random



for x in range(5):

    print(random.randint(init, end))



As you can see in the output, the randint() function could still give different random values. Now, let’s cut short the available range and see what happens.

import random



for x in range(5):

    print(random.randint(init, end))



Since the range was shorter and the number of calls was still 5, the randint Python function gave a repeated value: 5. Thus, the randint() function can provide the same number twice or even more times. There are also chances that you might get a repeated value, even if the available range is more extensive.

Looking forward to making a move to the programming field? Take up the Python Training Course and begin your career as a professional Python programmer

Summing It Up

In this article, you learned everything about the randint Python function. You also learned something fun, which was creating a lucky draw game that you can enjoy in your free time. You can also tweak that code to make it a bit more complex for a bigger lucky draw game. 

It is fascinating to see how one can do so much with Python programming. If you want to learn more about Python’s concepts, you can refer to Simplilearn’s Python Tutorial for Beginners. The tutorial is dedicated to newbies to help them get acquainted with Python’s basics. Once you are done with the basics, you can opt for our Online Python Certification Course to learn the advanced concepts and excel in Python development.

Have any questions for us? Leave them in the comments section, and our experts will answer them for you ASAP!

About the Author

Ravikiran A SRavikiran A S

Ravikiran A S works with Simplilearn as a Research Analyst. He an enthusiastic geek always in the hunt to learn the latest technologies. He is proficient with Java Programming Language, Big Data, and powerful Big Data Frameworks like Apache Hadoop and Apache Spark.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.
  • *According to Simplilearn survey conducted and subject to terms & conditions with Ernst & Young LLP (EY) as Process Advisors