Python Randint Function

The Python randint() inbuilt method returns a randomized value between the upper and the lower bounds provided as arguments. It is included in the random package. Before employing the randint() method, we must first integrate the random library into the program.
Let’s elaborate on the randint() function in Python.

Example 1:

In this instance, the randint() method creates any random number which lies in between the 40-80 range. Let’s see how it happens.

import random

n = random.randint(40,80)
print("The random number generated between 40 and 80 is",n)

First of all, we include a random header file because it deals with the randint() functionality. Then, we initialize the variable “n” and assign the value in the form of the randint() function. The randint() method produces any random number from this defined range. In the end, we utilize the print() method to display the line “The random number generated between 40 and 80 is” as well as the created integer.

Example 2:

In three different scenarios, this example employs the randint() method of Python to obtain a randomized value.

import random

n1 = random.randint(20, 30)
print("Random number between 20 and 30 is % s" % (n1))
n2 = random.randint(-20, -5)
print("Random number between -20 and -5 is % d" % (n2))
n3 = random.randint(-30, 30)
print("Random number between -30 and 30 is % d" % (n3))

At the start, we incorporate the random framework. Now, we declare the “n1” variable. We use the randint() function. The lower limits and the higher limits provided as the parameters of this function are both positive numbers. The randint() method creates the randomized value between these two positive values. The print() method prints that randomly created number.

Here, we once more declare the “n2” variable. The randint() function is used in this case. Both the lower and upper ranges offered as parameters for this function are negative numbers. Therefore, the random value between these two negative integers is created by the use of the randint() method. That generated number is then displayed by the print() function. We initialize a third variable – “n3”.

Then, we employ the randint() function. This function contains two arguments: the lower limit and the higher limit. Its lower limit is specified as a negative number and the upper limit is defined as a positive number. The random number is generated between the specified negative and positive ranges by using the randint() function. Simply printing that generated integer is all that the print() function does.

Example 3:

To imitate a guessing game, we may call the randint() method. Consider a scenario in which a user is given three attempts to select a number between 1 and 20 in a game. If the user’s prediction is accurate, he wins; otherwise, he loses.

import random

def GuessingGame():
        num = random.randint(1,20)
        guesses = 4
        flag = 0    
        print(' Let’s enjoy to the Guessing Game.!!')  
        while guesses> 0:
            guess = int(input("Enter the number: \n"))
            if guess == num:
                flag = 1
                print("Sike!! That number is incorrect. ")
            guesses = guesses- 1
        if flag == 1:
            return True
            return False
if GuessingGame() is True:
       print("Amazing!! You Predicted Correctly.")
else :
       print("Oopps! Try again later. ")

We begin the program by importing the random library. In the next step, we define the function of the name of the game – GuessingGame(). Then, the randint() function is employed. This function has the minimum limit and the maximum limit as its arguments. We indicate these limits here. The minimum limit is set to 1. Similarly, the maximum limit is set to 20. This method returns any random number which is present between 1 and 20. We initialize a “guesses” variable to define the number of guesses that the user gets.

Then, another variable named “flag” is also declared. This variable checks whether the user wins the game or not by guessing the right or wrong number. The print() function prints the welcome message on the screen. We utilize the while loop. We set the condition in the form that the guesses are greater than 0. This while loop executes ‘till the number of guesses last. The value is the integer data type. We set the “guess” variable to the “num” variable to check whether the guess entered by the user is equal to the num or not. If the user guesses the right number, we set the value of the “flag” variable to 1 and the while loop terminates. Otherwise, the print() method shows that the guessed value does not match. Here, we decrement in the number of guesses by 1.

Lastly, we again utilize the if-else statement. If the value of the GuessingGame() function is right, the print() statement prints the message which shows that the guess entered by the user is right. Otherwise, the print() statement prints the message which shows that the user guesses the wrong number.

Example 4:

Here, we’ll show how to find the ValueError when we call the randint() method.

import random

num1 = random.randint(5.55, 10.72)

After including the random module, we set the “num1” variable to its initial value and use the randint() method. Furthermore, we provide the floating-point values for the smaller range and higher range. Any random value from this specified range is generated by the randint() function. Finally, we employ the print() method to show the generated value.

Example 5:

This program indicates how to detect the TypeErrors by using the randint() method.

import random

num2 = random.randint('r', 'u')

Here, we introduce the random header file. In the next step, we declare a “num2” variable. The randint() function of the random library is called. This function contains two parameters which show the lower and higher range. The function randomly creates a value. Here, we pass the two alphabets as the limits. The print() method is called to show the result.


Everything there is to know about the randint() method is covered in this article. We executed the program where we used the randint() method and observed the results. Then, utilizing the functionality that we can play in our free time, we create a guessing game. The randint() function thereafter receives the floating-point values and alphabets as arguments. The function returns an error because it only ever accepts the arguments of the data type integer.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content