The random.triangular()
method in Python is used to return a random floating number, which is between two specified numbers or boundaries (both inclusive).
random.triangular(low,high,mode)
low
: This is a required parameter. We can specify the lower boundary of the random floating number, using this parameter. The default value for this parameter is 0.high
: This is a required parameter. We can specify the upper boundary of the random floating number, using this parameter. The default value for this parameter is 1.mode
: This is an optional parameter. It represents the midpoint between the two boundary values, high
and low
.Now, let us write a code to return a random floating number that is greater than or equal to 10, lesser than or equal to 30, but closer to a midpoint of 20.
import random print(random.triangular(10,30,20))
random
module.RELATED TAGS
CONTRIBUTOR
View all Courses