randint() Function in Python
Overview
randint() is a built-in function of the random module in Python that returns a random integer between the higher and lower limit passed as parameters. randint() takes only integer type parameters and generates an integer type random value.
Syntax of randint() function in Python
randint() function in python can only generate integer-type random values.
Parameters of randint() function in python
randint() function in Python includes two parameters:
- start parameter, which is the starting point from and including which the random integer would be generated,
- end parameter, which is the ending point up to which the function would return the random integer.
Both start and end must be integer type values.
Return Values of randint() function in Python
randint() function in Python returns a random integer in the range [start, end] including both start and end points.
Exceptions of randint() function in python
There are two types of exceptions faced by the randint() function in Python:
- ValueError : returned when floating point values are passed as parameters.
- TypeError : returned when anything other than numeric values are passed as parameters.
Example of randint() fucntion in Python
Code:
Output:
Explanation: In the above program, the randint() function generates a random integer value within the limit of 20-50.
What is randint() function in Python?
The randint() function in python is a built-in function of the random module that returns a random integer between the higher and lower limit passed as parameters. The randint() function returns an integer N where N>=beg and N<=end.
We must first import the random module in Python to begin using the randint() function. The module essentially creates pseudo-randomness.
Learn more about random modules from this article.
Application of randint() function in Python
The randint() function in python can simulate a Guessing Game. Let’s say a user is playing a game in which he gets three chances to guess the number between 1 and 10. If the guessed answer is correct, the user wins, else loses the game.
Code:
Output:
More Examples
Example 1: Working of randint() function
Code:
Output:
Explanation: The above program generates a random integer using the randint() function in python in 3 different cases that include
- Case 1: Generates a random number between two positive integers
- Case 2: Generates a random number between two negative integers.
- Case 3: Generates a random number between one positive and other negative integer.
Example 2: ValueError in randint() function
Code:
Output:
Explanation:
The above program demonstrates how we can get ValueError while using randint() function in python. We get this error because we passed floating point values as parameters in the randint() function.
Example 3: TypeError in randint() function
Code:
Output:
Explanation: The above program demonstrates how we can get TypeError while using randint() function in python. We got this error because we passed string or character literals that are non-numeric values as parameters in the randint() function.
Click Here, To know about the int() function in python.
Conclusion
- randint() function is used to generate the random integers between the two given numbers passed as parameters.
- Randint is an in-built function of random library in python. So we have to import the random library at the start of our code to generate random numbers.
- randint function returns:
- ValueError, when floating point values are passed as parameters.
- TypeErrorwhen anything other than numeric values is passed as parameters.