.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 betweenlow
andhigh
).
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
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours