Random randint() Method in Python:
The randint() method returns an integer number representing a randomly chosen element from the specified range.
Note: It is to be noted that randint() method is an alias for randrange(start, stop+1).
Syntax:
random.randint(start, stop)
Parameters
start: This is Required. It is an integer indicating the starting position.
stop: This is Required. It is an integer indicating the position at which to end.
Examples:
Example1:
Input:
Given start value = 4 Given stop value = 12
Output:
The random number between 4 and 12 = 12 (includes both 4 and 12)
Example2:
Input:
Given start value = 40 Given stop value = 45
Output:
The random number between 40 and 45 = 42 (includes both 40 and 45)
Random randint() Method with Examples in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Import random module using the import keyword.
- Give the number(start value) as static input and store it in a variable.
- Give the other number(stop value) as static input and store it in another variable.
- Pass the given start and stop values as the arguments to the random.randint() method to get a random number between the given start and stop values ( both start and stop values are included).
- Store it in another variable.
- Print a random number between the given start and stop values.
- The Exit of the Program.
Below is the implementation:
# Import random module using the import keyword. import random # Give the number(start value) as static input and store it in a variable. gvn_strtval = 4 # Give the other number(stop value) as static input and store it in another variable. gvn_stopval = 12 # Pass the given start and stop values as the arguments to the random.randint() # method to get a random number between the given start and stop values. # ( both start and stop values are included). # Store it in another variable. rslt = random.randint(gvn_strtval, gvn_stopval) # Print a random number between the given start and stop values. print("The random number between", gvn_strtval, "and", gvn_stopval, "=", rslt)
Output:
The random number between 4 and 12 = 12
Method #2: Using Built-in Functions (User Input)
Approach:
- Import random module using the import keyword.
- Give the number(start value) as user input using the int(input()) function and store it in a variable.
- Give the other number(stop value) Â as user input using the int(input()) function and store it in another variable.
- Pass the given start and stop values as the arguments to the random.randint() method to get a random number between the given start and stop values ( both start and stop values are included).
- Store it in another variable.
- Print a random number between the given start and stop values.
- The Exit of the Program.
Below is the implementation:
# Import random module using the import keyword. import random # Give the number(start value) as user input using the int(input()) function and store it in a variable. gvn_strtval = int(input("Enter some random number = ")) # Give the other number(stop value) as user input using the int(input()) function # and store it in another variable. gvn_stopval = int(input("Enter some random number = ")) # Pass the given start and stop values as the arguments to the random.randint() # method to get a random number between the given start and stop values. # ( both start and stop values are included). # Store it in another variable. rslt = random.randint(gvn_strtval, gvn_stopval) # Print a random number between the given start and stop values. print("The random number between", gvn_strtval, "and", gvn_stopval, "=", rslt)
Output:
Enter some random number = 40 Enter some random number = 45 The random number between 40 and 45 = 42