Python Random randrange() Method with Examples

Random randrange() Method in Python:

The randrange() method selects an element at random from the specified range and returns it.

Syntax:

random.randrange(start, stop, step)

Parameters

start: This is Optional. It is an integer indicating the starting position.
0 is the default.

stop: This is Required. It is an integer indicating the position at which to end.

step: Optional. The incrementation is specified by an integer.
1 is the default.

Examples:

Example1:

Input:

Given start value = 1
Given stop value = 5

Output:

The random number between 1 and 5 = 3

Example2:

Input:

Given start value = 1
Given stop value = 10
Given step size = 2

Output:

The random number between 1 and 10 = 7

Explanation:

Here it prints a random odd number between 1 and 10(includes only start value) 
since the start value is 1 and stepsize is 2

Random randrange() 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.randrange() method to get a random number between the given start and stop values (includes only start value).
  • 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 = 1
# Give the other number(stop value) as static input and store it in another variable.
gvn_stopval = 5
# Pass the given start and stop values as the arguments to the random.randrange()
# method to get a random number between the given start and stop values.
# (includes only start value)
# Store it in another variable.
rslt = random.randrange(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 1 and 5 = 3
with giving step value

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.
  • Give the step size as static input and store it in another variable.
  • Pass the given start, stop and step values as the arguments to the random.randrange() method to get a random number between the given start and stop values(includes only start value) with a given step size.
  • 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 = 1
# Give the other number(stop value) as static input and store it in another variable.
gvn_stopval = 10
# Give the step size as static input and store it in another variable.
gvn_step = 2
# Pass the given start,stop and step values as the arguments to the
# random.randrange() method to get a random number between the given start and
# stop values(includes only start value) with a given stepsize.
# Store it in another variable.
rslt = random.randrange(gvn_strtval, gvn_stopval, gvn_step)
# 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 1 and 10 = 7

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.randrange() method to get a random number between the given start and stop values (includes only start value).
  • 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.randrange()
# method to get a random number between the given start and stop values.
# (includes only start value)
# Store it in another variable.
rslt = random.randrange(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 = 10
Enter some random number = 17
The random number between 10 and 17 = 10
with giving step value

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.
  • Give the step size as static input and store it in another variable.
  • Pass the given start, stop and step values as the arguments to the random.randrange() method to get a random number between the given start and stop values(includes only start value) with a given step size.
  • 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 = "))
# Give the step size as user input using the int(input()) function and store it in another variable.
gvn_step = int(input("Enter some random number = "))
# Pass the given start,stop and step values as the arguments to the
# random.randrange() method to get a random number between the given start and
# stop values(includes only start value) with a given stepsize.
# Store it in another variable.
rslt = random.randrange(gvn_strtval, gvn_stopval, gvn_step)
# 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 = 50
Enter some random number = 60
Enter some random number = 3
The random number between 50 and 60 = 53