❮ Random Methods
Example
Return a random number between, and included, 20 and 60, but most likely closer to 20:
import random
print(random.triangular(20, 60, 30))
Try it Yourself »
Definition and Usage
The triangular() method returns a random floating number between the two specified numbers (both included), but you can also specify a third parameter, the mode parameter.
The mode parameter gives you the opportunity to weigh the possible outcome closer to one of the other two parameter values.
The mode parameter defaults to the midpoint between the two other parameter values, which will not weigh the possible outcome in any direction.
Syntax
random.triangular(low, high, mode)
Parameter Values
low | Optional. A number specifying the lowest possible outcome. Default 0 |
high | Optional. A number specifying the highest possible outcome. Default 1 |
mode | Optional. A number used to weigh the result in any direction. Default the midpoint between the low and high values |
❮ Random Methods
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
triangular() is an inbuilt method of the random module. It is used to return a random floating point number within a range with a bias towards one extreme.
Syntax : random.triangular(low, high, mode)
Parameters :
low : the lower limit of the random number
high : the upper limit of the random number
mode : additional bias; low < mode < highif the parameters are (10, 100, 20) then due to the bias, most of the random numbers generated will be closer to 10 as opposed to 100.
Returns : a random floating number
Example 1:
import random
low = 10
high = 100
mode = 20
print(random.triangular(low, high, mode))
Output :
22.614510550239572Example 2: If we generate the number multiple times we can probably identify the bias.
import random
low = 10
high = 100
mode = 20
for i in range(10):
print(random.triangular(low, high, mode))
Output :
58.645768016894735 46.690692250503226 33.57590419190895 52.331804090351305 33.09451214875767 12.03845752596168 32.816080679206294 20.4739124559502 82.49208123077557 63.511062284733015Example 3: We can visualize the triangular pattern by plotting a graph.
import random
import matplotlib.pyplot as plt
nums = []
low = 10
high = 100
mode = 20
for i in range(10000):
temp = random.triangular(low, high, mode)
nums.append(temp)
plt.hist(nums, bins = 200)
plt.show()
Output :
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
With the help of numpy.random.triangular() method, we can get the random samples from triangular distribution from interval [left, right] and return the random samples by using this method.
Syntax : numpy.random.triangular(left, mode, right, size=None)
Parameters :
1) left – lower limit of the triangle.
2) mode – peak value of the distribution.
3) right – upper limit of the triangle.
4) size – total number of samples required.
Return : Return the random samples as numpy array.
Example #1 :
In this example we can see that by using numpy.random.triangular() method, we are able to get the random samples of triangular distribution and return the numpy array.
Python3
import numpy as np
import matplotlib.pyplot as plt
gfg = np.random.triangular(-5, 0, 5, 5000)
plt.hist(gfg, bins = 50, density = True)
plt.show()
Output :
Example #2 :
Python3
import numpy as np
import matplotlib.pyplot as plt
gfg = np.random.triangular(-10, 8, 10, 15000)
plt.hist(gfg, bins = 100, density = True)
plt.show()
Output :