Table of contents
1.
Introduction
2.
What is randint() in Python?
2.1.
Syntax
2.2.
Parameters
2.3.
Return Value
2.4.
Errors and Exceptions
3.
Examples
3.1.
Randint with Positive and Negative integrals
3.1.1.
Code in Python
3.2.
Randint for Decimal or Floating Point values
3.2.1.
Code in Python
3.3.
Randint for Non-numeric Values like String or Character Values
3.3.1.
Code in Python
4.
Applications
4.1.
Code in Python: Lottery System
5.
Frequently Asked Questions
5.1.
What is the range of values that the randint function can generate?
5.2.
Can randint generate floating-point numbers?
5.3.
How can I generate a random integer in Python without using randint?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

randint() Function in Python

Author Avni Gupta
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Randomness is a crucial component in computer science, and it is often used in various applications, such as cryptography, simulation, and games. 

Python language provides a built-in random module that allows developers to generate random numbers and sequences. One of the most frequently used functions in the random module is randint(). 

If you're a Python developer looking to incorporate randomness into your code, the randint function is an excellent place to start.

randint() function in python

In this blog, we will learn about the syntax and key points we need to know about the randint function in Python, along with its applications. We will also look at some commonly asked questions on the same.

What is randint() in Python?

The randint() function in Python generates a random integer between two specified values. 

Input: The function takes two arguments, a and b, which represent the lower and upper bounds of the random integer to be generated. The values must be of integer data type.

Output: The function returns a random integer n such that a <= n <= b.

Syntax

The syntax for using the randint function after importing the random library  is as follows: 

random.randint(a,b)

Parameters

  • a - The lower value of the range indicates the least number which can be generated
     
  • b - The upper value of the range indicating the maximum number which can be generated
     
  • (a,b) - the range in which the random number will lie

Return Value

This function returns a random number between a and b with both a and b included.

Errors and Exceptions

  • ValueError - This is returned if a non-integral value, like a floating point value, is passed.
     
  • TypeError - This is returned if a non-numeric value like a string value is passed.

Examples

Randint with Positive and Negative integrals

Code in Python

import random
print(“Random value between 1 and 100 :”, random.randint(1,100))
print(“Random value between -20 and 0 :”,random.randint(-20,0))

 

Output

Random value between 1 and 100 : 70
Random value between -20 and 0 : -3

Randint for Decimal or Floating Point values

Code in Python

import random
print("Random value between 23.5 and 68.7 :",random.randint(23.5,68.7))

 

Output

output for Randint for Decimal or Floating Point values showing error

Randint for Non-numeric Values like String or Character Values

Code in Python

import random
print("Random value between a and b :",random.randint('a','b'))

 

Output

output for Randint for Non-numeric Values like String or Character Values

Applications

The randint function finds its application in many fields, such as gaming, simulations, and cryptography. 

  • In gaming, the randint function simulates dice rolls, coin tosses, and other random events.
     
  • In simulations, the randint function can generate random values for physical system variables like position, velocity, and acceleration. 
     
  • In cryptography, the randint function generates random keys and nonces. Cryptography heavily relies on randomness, and the ability to generate truly random values is essential in ensuring cryptographic systems' security.
     

Here is a lottery system in which we use the randint() function.

Code in Python: Lottery System

#importing random library for using randint
import random

# Define the range of numbers for the lottery
MIN_NUMBER = 1
MAX_NUMBER = 50

# Define the number of lottery balls to draw
NUM_BALLS = 6

#We will keep adding the randomly generated number to the list created below
winning_numbers = []


#the loop will run the name number of times are the number of balls
while len(winning_numbers) < NUM_BALLS:
    #generating the random winning number
    number = random.randint(MIN_NUMBER, MAX_NUMBER)
    #check if the number is already in the list
    if number not in winning_numbers:
        #add the number to the list if it is not already present 
        winning_numbers.append(number)


# Sort the winning numbers in ascending order
winning_numbers.sort()


# Print the winning numbers
print("The winning numbers are:")
for number in winning_numbers:
    print(number)


Output

The winning numbers are:
14
18
24
27
28
49

 

This code generates a set of six random numbers between 1 and 50, sorts them in ascending order, and prints them out as the winning numbers for the lottery. 

You can modify the values of MIN_NUMBER, MAX_NUMBER, and NUM_BALLS to change the range of numbers and the number of balls drawn for your specific lottery game.

Also see, Convert String to List Python

Frequently Asked Questions

What is the range of values that the randint function can generate?

The range of values that the randint function can generate includes both the lower and upper bounds. For example, if the lower bound is 1 and the upper bound is 10, the randint function can generate any integer between 1 and 10, including 1 and 10.

Can randint generate floating-point numbers?

No, randint can only generate integers and not floating-point numbers. If you need to generate random floating-point numbers, you can use the random.random function instead of the randint function. 

How can I generate a random integer in Python without using randint?

You can use the randrange function from the random module to generate a random integer within a range. For example, random.randrange(1, 10) generates a random integer between 1 and 9.

Conclusion

The randint function in Python is a powerful tool for generating random integers between two specified values. It is an essential function in the random module and is frequently used in various applications, such as gaming, simulations, and cryptography. 

Check out the following blogs:

Check out some amazing Guided Paths on topics such as Basics of PythonOops in Python, Basics of C, etc., along with some Contests and Interview Experiences only on Coding Ninjas Studio

Do check out The Interview Guide for Product Based Companies, as well as some of the Popular Interview Problems from Top companies like AmazonAdobeGoogle, etc., on Coding Ninjas Studio.
Happy Learning!!

Live masterclass