randint function in Python
In this post, you will learn about the randint() function of Python.
Python Random Module
There is an in-built random module in Python that's used to generate pseudo-random variables. We can use this to perform some activities randomly, for example, getting a random number, choosing an irregular element from a list, randomly shuffle elements, and so on. This module provides several functions to perform operations randomly. Here, you will know how to select an element randomly.
The randint() is an in-built function of the random module in Python3. The randint() method returns an integer number from the specified range.
randint(start, end)
Here, the start and end parameters must be integer type values. It returns the random integer in the range [start, end], including both starting and ending numbers.
import random as rnd
# Generate the random number between any positive number range
random1 = rnd.randint(25, 85)
print ("The any random number between 25 and 85 is % s" % (random1))
# Generate the random number between two given negative number range
random2 = rnd.randint(-30, -15)
print ("The any random number between -30 and -15 is % s" % (random2))
# Generate the random number between a positive number and a negative number range
random3 = rnd.randint(-10, 15)
print ("The any random number between -10 and 15 is % s" % (random3))
Output of the above code:
The any random number between 25 and 85 is 42
The any random number between -30 and -15 is -26
The any random number between -10 and 15 is 11
Errors and Exceptions
- ValueError: It returns a ValueError, when the user passes floating point as parameters.
- TypeError: It returns a TypeError, when anything other than numeric values are passed as parameters.
ValueError Example
The given program demonstrates how the users can get the ValueError while using the randint() function. Here, we have passed floating point values as parameters in the randint() function.
# imports random module
import random
randnum = random.randint(1.25, 9.14)
print(randnum)
Output of the above code:
Traceback (most recent call last):
File "", line 4, in
File "/usr/lib/python3.8/random.py", line 248, in randint
return self.randrange(a, b+1)
File "/usr/lib/python3.8/random.py", line 212, in randrange
raise ValueError("non-integer arg 1 for randrange()")
ValueError: non-integer arg 1 for randrange()
TypeError Example
The given program demonstrates how the users can get the TypeError while using the randint() function. Here, we have passed string or character literals as parameters in the randint() function.
# imports random module
import random
randnum = random.randint('a', 'z')
print(randnum)
Output of the above code:
Traceback (most recent call last):
File "", line 4, in
File "/usr/lib/python3.8/random.py", line 248, in randint
return self.randrange(a, b+1)
TypeError: can only concatenate str (not "int") to str
Related Articles
Convert Python list to numpy arrayConvert string to list Python
Python program to list even and odd numbers of a list
Python loop through list
Sort list in descending order Python
Convert array to list Python
Python take screenshot of specific window
Web scraping Python BeautifulSoup
Check if two strings are anagrams Python
Python program to add two numbers
Print new line python
Python for loop index
Convert List to Dataframe Python
numpy random choice
Dictionary inside list python
Check if list is empty Python
Python raise keyword
Python program to get the largest number from a list
Python program to map two lists into a dictionary