Python random.triangular() Method



The random.triangular() method in Python generates a random floating point number within a range defined by the parameters low and high. This function follows a triangular distribution, where values are spread between a minimum (low) and a maximum (high), with the peak at the mode. This means that values near the mode are more likely to be selected, but all values between low and high are possible.

By default, the low and high parameters are set to 0 and 1, respectively, and the mode is set to the midpoint between low and high, resulting in a symmetric distribution.

Syntax

Following is the syntax of triangular() method −

random.triangular(low, high, mode)

Parameters

The Python random.triangular() method accepts the below parameter −

  • low: The lower bound of the distribution.

  • high: The upper bound of the distribution.

  • mode: The most likely value (the peak of the distribution) between low and high.

Return Value

This random.triangular() method returns a random floating-point number that follows a triangular distribution.

Example 1

Let's see a basic example of using the random.triangular() method for generating a single random floating point number.

import random

# Parameters
low = 0
high = 100

# Generate a single random floating point number
random_value = random.triangular(low, high)

print("Generated random floating point number:", random_value)

Following is the output −

Generated random floating point number: 50.941793194646195

Note: The Output generated will vary each time you run the program due to its random nature.

Example 2

This example generates a list of random floating-point numbers using the random.triangular() method. Each resultant value follows the triangular distribution between 0 and 100, with the peak (mode) at 10.

import random

# Parameters
low = 0
high = 100
mode = 5

# Generate multiple random floating point numbers
random.seed(100)
sample = [random.triangular(low, high, mode) for _ in range(10)]

print("Generated random floating point number:", sample)

While executing the above code you will get the similar output like below −

Generated random floating point number: [9.910366431476916, 28.04033451219499, 53.33573266149688, 47.107426380192706, 49.53823470998988, 26.64052298105129, 56.413239886033786, 33.38591319476359, 6.519842946194032, 28.107621205331426]

Example 3

Here is another example that uses the random.triangular() method to generate a list of 1000 random floating-point numbers from a triangular distribution between 0 and 100, with the peak (mode) closer to 95. It then calculates and prints the average of these numbers.

import random

# Parameters
low = 0
high = 100
mode = 95  # Closer to high

# Generate a sample
random.seed(100)
sample = [random.triangular(low, high, mode) for _ in range(1000)]

# Display the average
print('Average:', round(sum(sample) / len(sample), 2))

The output of the above code is as follows −

Average: 63.96
python_modules.htm
Advertisements