.triangular()

Anonymous contributor's avatar
Anonymous contributor
Published Aug 28, 2024
Contribute to Docs

In Python, the .triangular() method returns a random floating-point number from a triangular distribution. The input parameters are low, high, and mode. If mode is not provided, it defaults to the midpoint between low and high.

Syntax

random.triangular(low, high, mode)
  • low: The lower limit of the distribution (optional; defaults to 0.0).
  • high: The upper limit of the distribution (optional; defaults to 1.0).
  • mode: The peak of the distribution (optional; defaults to the midpoint between low and high).

Example

In the example below, the .triangular() method is used to return a random integer between 50 and 500, biased towards 400:

import random
print(random.triangular(50,500,400))

The code above produces the following output:

395

Note: The output value will vary with each execution because .triangular() generates a random number within the specified distribution.

Codebyte Example

The following codebyte example demonstrates how the .triangular() method generates random values with different biases:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn Python on Codecademy