
135 views
randint() Function in Python
The Python randint()
function is part of the random
module and is used to generate random integers within a specified range. It returns a random integer between the specified minimum (inclusive) and maximum (inclusive) values.
Here’s the basic syntax of the randint()
function:
import random
random_integer = random.randint(minimum, maximum)
minimum
: The minimum integer in the range (inclusive).maximum
: The maximum integer in the range (inclusive).
Here’s an example of how to use the randint()
function:
import random
# Generate a random integer between 1 and 10 (inclusive)
random_number = random.randint(1, 10)
print("Random number:", random_number)
In this example, random.randint(1, 10)
will generate a random integer between 1 and 10, including both 1 and 10.
You can use the randint()
function whenever you need random integers for various purposes, such as simulating dice rolls, random selections, or generating random test data.