Javatpoint Logo
Javatpoint Logo

The randint() Function in Python

In this tutorial, we will learn about the "randint()" function in Python.

The "randint()" is a built-in function of the random module in Python. The random module is used for getting access to various functions like generating random numbers by using the randint() function.

First, we have to import the random module in Python to use the randint() function. This function is basically used for creating pseudo-randomness.

Syntax:

Parameters:

(start_range, end_range): Both the parameters must be integer type values.

Parameters:

It will return the random integer in the range [start_range, end_range], including both starting and ending numbers.

Errors and Exceptions:

ValueError: Returns a ValueError when the user passes floating point as parameters.

TypeError: Returns a TypeError when the user passes anything other than integer value as parameters.

Example 1:

To get the random number between the given range of two positive numbers, two negative numbers, and one positive and one negative number.

Output:

1#

The any random number between 55 and 75 is 74
The any random number between -40 and -20 is -40
The any random number between -20 and 20 is -12

2#

The any random number between 55 and 75 is 74
The any random number between -40 and -20 is -29
The any random number between -20 and 20 is -2

Example 2:

In this example, we will see how the users can get the ValueError in the Python program while using the randint() function.

Output:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
 in 
      4 # If the user passes any floating point values as the parameters in the randint() function.
      5 
----> 6 random_1 = rnd.randint(2.543, 12.786)
      7 print(random_1)

c:\users\User Name\appdata\local\programs\python\python39\lib\random.py in randint(self, a, b)
    336         """
    337 
--> 338         return self.randrange(a, b+1)
    339 
    340 

c:\users\user name\appdata\local\programs\python\python39\lib\random.py in randrange(self, start, stop, step)
    300         istart = int(start)
    301         if istart != start:
--> 302             raise ValueError("non-integer arg 1 for randrange()")
    303         if stop is None:
    304             if istart > 0:

ValueError: non-integer arg 1 for randrange()

Example 3:

In this example, we will see how the user can get the TypeError in Python while using the randint() function.

Output:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
 in 
      4 # If the user passes any string or character value as the parameters in the randint() function
      5 
----> 6 random_2 = rnd.randint('String', 'Character')
      7 print (random_2)

c:\users\user name\appdata\local\programs\python\python39\lib\random.py in randint(self, a, b)
    336         """
    337 
--> 338         return self.randrange(a, b+1)
    339 
    340 

TypeError: can only concatenate str (not "int") to str

Applications:

The users can use the randint() function for simulating a lucky draw game.

Suppose we have participated in a lucky draw game such as "Casino Game". The player will get three chances to guess the number between 1 to 36. If we have guessed the number correctly, we will win, else we will lose the game.

Example: code for application

Output:

1#

Please select your number to enter the lucky draw game 
 3
You have guessed Wrong Number!!
Please select your number to enter the lucky draw game 
 2
You have guessed Wrong Number!!
Please select your number to enter the lucky draw game 
 34
You have guessed Wrong Number!!
Sorry, you have Lost the game!

2#

Please select your number to enter the lucky draw game 
 14
You have guessed Wrong Number!!
Please select your number to enter the lucky draw game 
 12
You have guessed Wrong Number!!
Please select your number to enter the lucky draw game 
 3
Congratulation!! You have Won the game.

Conclusion

In this tutorial, we have discussed the randint() function of Python's random module. We have shown the types of errors the user can get while using the randint() function. We have also discussed how the randint() function is used for creating an application of lucky draw games.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA