numpy.random.Generator.triangular#
method
- random.Generator.triangular(left,mode,right,size=None)#
Draw samples from the triangular distribution over theinterval
[left,right].The triangular distribution is a continuous probabilitydistribution with lower limit left, peak at mode, and upperlimit right. Unlike the other distributions, these parametersdirectly define the shape of the pdf.
- Parameters:
- leftfloat or array_like of floats
Lower limit.
- modefloat or array_like of floats
The value where the peak of the distribution occurs.The value must fulfill the condition
left<=mode<=right.- rightfloat or array_like of floats
Upper limit, must be larger thanleft.
- sizeint or tuple of ints, optional
Output shape. If the given shape is, e.g.,
(m,n,k), thenm*n*ksamples are drawn. If size isNone(default),a single value is returned ifleft,mode, andrightare all scalars. Otherwise,np.broadcast(left,mode,right).sizesamples are drawn.
- Returns:
- outndarray or scalar
Drawn samples from the parameterized triangular distribution.
Notes
The probability density function for the triangular distribution is
\[\begin{split}P(x;l, m, r) = \begin{cases}\frac{2(x-l)}{(r-l)(m-l)}& \text{for $l \leq x \leq m$},\\\frac{2(r-x)}{(r-l)(r-m)}& \text{for $m \leq x \leq r$},\\0& \text{otherwise}.\end{cases}\end{split}\]The triangular distribution is often used in ill-definedproblems where the underlying distribution is not known, butsome knowledge of the limits and mode exists. Often it is usedin simulations.
References
[1]Wikipedia, “Triangular distribution”https://en.wikipedia.org/wiki/Triangular_distribution
Examples
Draw values from the distribution and plot the histogram:
>>>importmatplotlib.pyplotasplt>>>rng=np.random.default_rng()>>>h=plt.hist(rng.triangular(-3,0,8,100000),bins=200,...density=True)>>>plt.show()
