Use the randint()
Function
#import the random module
import random
#get a random integer between 0 and 100
print(random.randint(0, 100))
The
randint()
function in the random module generates a random integer within a specified range.
randint(start, stop)
The two parameters; start
and stop
represent the lower and an upper bound (inclusive of both) of the range. Both parameters should be strictly integers.
start |
Required. Represents the start of the range of the number to be generated |
stop |
Required. Specifies the end of the range of the number to be generated |
This function returns a random integer in the range specified by the start and stop parameters.
get a random integer between 20 and 30(inclusive)
#Import the module
import random
print(random.randint(20, 30))
The function is useful for generating random numbers for simulations, games, or any other application where random numbers are needed. It is also useful for data analysis and machine learning tasks where we need data samples drawn from a certain range of numbers.
Generate a list of random integers using randint()
We can append the generated integers to a list so as to get a list of random integers.
Get a list of random integers between 0 and a 100
#import the module
import random
random_ints = []
for i in range(10):
random_ints.append(random.randint(0, 100))
print(random_ints)