Python Random triangular() Method with Examples

Random triangular() Method in Python:

The triangular() method returns a random floating number between the two specified numbers (both included), but a third parameter, the mode parameter, can also be specified.

The mode parameter allows you to weigh the possible outcomes in relation to one of the other two parameter values.

The mode parameter is set to the midpoint between the other two parameter values, and it does not weigh the possible outcomes in any direction.

Syntax:

random.triangular(low, high, mode)

Parameters

low: This is Optional. A number indicating the lowest possible outcome.
0 is the default.

high: This is Optional. A number indicating the highest possible outcome.
1 is the default.

mode: This is Optional. A number that is used to weigh the outcome in any direction.
Set the default value to the midpoint between the low and high values.

Examples:

Example1:

Input:

Given first number = 10
Given second number = 40
Given mode = 15

Output:

The random floating-point number between 10 and 40 = 16.45838431286139

Example2:

Input:

Given first number = 5
Given second number = 15
Given mode = 8

Output:

Enter some random number = 5
Enter some random number = 15
Enter some random number = 8
The random floating-point number between 5 and 15 = 6.479018408138248

Random triangular() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import random module using the import keyword.
  • Give the number(low value) as static input and store it in a variable.
  • Give the other number(high value) as static input and store it in another variable.
  • Give the mode value as static input and store it in another variable.
  • Pass the given low, high, mode values as the arguments to the random.triangular() method that returns a random floating number between the given two specified numbers (both included).
  • Store it in another variable.
  • Print a random floating-point number between the given two numbers (both included).
  • The Exit of the Program.

Below is the implementation:

# Import random module using the import keyword.
import random
# Give the number(low value) as static input and store it in a variable.
gvn_fstnum = 10
# Give the other number(high value) as static input and store it in
# another variable.
gvn_scndnum = 40
# Give the mode value as static input and store it in another variable.
gvn_mode = 15
# Pass the given low, high, mode values as the arguments to the random.triangular()
# method that returns a random floating number between the given two specified
# numbers (both included).
# Store it in another variable.
rslt = random.triangular(gvn_fstnum, gvn_scndnum, gvn_mode)
# Print a random floating-point number between the two numbers (both included).
print("The random floating-point number between",
      gvn_fstnum, "and", gvn_scndnum, "=", rslt)

Output:

The random floating-point number between 10 and 40 = 16.45838431286139

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import random module using the import keyword.
  • Give the number(low value) as user input using the int(input()) function and store it in a variable.
  • Give the other number(high value) as user input using the int(input()) function and store it in another variable.
  • Give the mode value as user input using the int(input()) function and store it in another variable.
  • Pass the given low, high, mode values as the arguments to the random.triangular() method that returns a random floating number between the given two specified numbers (both included).
  • Store it in another variable.
  • Print a random floating-point number between the given two numbers (both included).
  • The Exit of the Program.

Below is the implementation:

# Import random module using the import keyword.
import random
# Give the number(low value) as user input using the int(input()) function 
# and store it in a variable.
gvn_fstnum = int(input("Enter some random number = "))
# Give the other number(high value) as user input using the int(input()) function
# and store it in another variable.
gvn_scndnum = int(input("Enter some random number = "))
# Give the mode value as user input using the int(input()) function and 
# store it in another variable.
gvn_mode = int(input("Enter some random number = "))
# Pass the given low, high, mode values as the arguments to the random.triangular()
# method that returns a random floating number between the given two specified
# numbers (both included).
# Store it in another variable.
rslt = random.triangular(gvn_fstnum, gvn_scndnum, gvn_mode)
# Print a random floating-point number between the two numbers (both included).
print("The random floating-point number between",
      gvn_fstnum, "and", gvn_scndnum, "=", rslt)

Output:

Enter some random number = 5
Enter some random number = 15
Enter some random number = 8
The random floating-point number between 5 and 15 = 6.479018408138248