Python .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.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

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

All contributors

Contribute to Docs

Learn Python on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours