Python .triangular()
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 betweenlowandhigh).
Example
In the example below, the .triangular() method is used to return a random integer between 50 and 500, biased towards 400:
import randomprint(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:
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve 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