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.

Learn the Ins & Outs of Software Development

Caltech Coding BootcampExplore Program
Learn the Ins & Outs of Software Development

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))

Output:

RandintInPython_1

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.

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)

print(int1)

Output:

RandintInPython_2.

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

Here's How to Land a Top Software Developer Job

Full Stack Developer - MERN StackExplore Program
Here's How to Land a Top Software Developer Job

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')

print(int1)

Output:

RandintInPython_3

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.

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

break

else:

# 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

else:

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!")

Output:

RandintInPython_4.

Second attempt output:

RandintInPython_5

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.

Learn the Ins & Outs of Software Development

Caltech Coding BootcampExplore Program
Learn the Ins & Outs of Software Development

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.

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

init=1

end=50

for x in range(5):

    print(random.randint(init, end))

Output:

RandintInPython_6

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

init=1

end=10

for x in range(5):

    print(random.randint(init, end))

Output:

RandintInPython_7

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.

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 Post Graduate Program in Full Stack Web Development 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!

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 24 Feb, 2025

6 Months$ 8,000
Automation Test Engineer Masters Program

Cohort Starts: 8 Jan, 2025

8 months$ 1,499
Full Stack Java Developer Masters Program

Cohort Starts: 8 Jan, 2025

7 months$ 1,449
Full Stack (MERN Stack) Developer Masters Program

Cohort Starts: 8 Jan, 2025

6 Months$ 1,449