The Wayback Machine - https://web.archive.org/web/20240525002952/https://www.geeksforgeeks.org/random-triangular-method-in-python/
Open In App

random.triangular() method in Python

Last Updated : 17 May, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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 < high

if 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 the random module
import random
  
# determining the values of the parameters
low = 10
high = 100
mode = 20
  
# using the triangular() method
print(random.triangular(low, high, mode))


Output :

22.614510550239572

Example 2: If we generate the number multiple times we can probably identify the bias.




# import the random module
import random
  
# determining the values of the parameters
low = 10
high = 100
mode = 20
  
# running the triangular method with the
# same parameters multiple times
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.511062284733015

Example 3: We can visualize the triangular pattern by plotting a graph.




# import the required libraries
import random
import matplotlib.pyplot as plt
  
  
# store the random numbers in a list
nums = []
low = 10
high = 100
mode = 20
  
for i in range(10000):
    temp = random.triangular(low, high, mode)
    nums.append(temp)
      
# plotting a graph
plt.hist(nums, bins = 200)
plt.show()


Output :



Previous Article
Next Article

Similar Reads

numpy.random.triangular() in Python
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)
1 min read
Python Random - random() Function
There are certain situations that involve games or simulations which work on a non-deterministic approach. In these types of situations, random numbers are extensively used in the following applications: Creating pseudo-random numbers on Lottery scratch cardsreCAPTCHA on login forms uses a random number generator to define different numbers and ima
3 min read
Random sampling in numpy | random() function
numpy.random.random() is one of the function for doing random sampling in numpy. It returns an array of specified shape and fills it with random floats in the half-open interval [0.0, 1.0). Syntax : numpy.random.random(size=None) Parameters : size : [int or tuple of ints, optional] Output shape. If the given shape is, e.g., (m, n, k), then m * n *
2 min read
NumPy random.noncentral_f() | Get Random Samples from noncentral F distribution
The NumPy random.noncentral_f() method returns the random samples from the noncentral F distribution. Example C/C++ Code import numpy as np import matplotlib.pyplot as plt gfg = np.random.noncentral_f(1.24, 21, 3, 1000) count, bins, ignored = plt.hist(gfg, 50, density = True) plt.show() Output: Syntax Syntax: numpy.random.noncentral_f(dfnum, dfden,
1 min read
Python | Checking triangular inequality on list of lists
Given a list of lists, the task is to find whether a sublist satisfies the triangle inequality. The triangle inequality states that for any triangle, the sum of the lengths of any two sides must be greater than or equal to the length of the remaining side. In other words, a triangle is valid if sum of its two sides is greater than the third side. I
3 min read
Python - Triangular Distribution in Statistics
scipy.stats.triang () is a triangular continuous random variable. It is inherited from the of generic methods as an instance of the rv_continuous class. It completes the methods with details specific for this particular distribution. Parameters : q : lower and upper tail probability x : quantiles loc : [optional]location parameter. Default = 0 scal
2 min read
Create a pseudocolor plot of an unstructured triangular grid in Python using Matplotlib
Matplotlib is a Python module which can be used for data visualization; and analyse the data graphically in the form of pie-chart, histogram, bar graph and many more. It also has an ability to generate MATLAB-like frameworks. Unstructured triangular grid An unstructured triangular grid contains n_points points and n_tri triangles which can either b
5 min read
sympy.stats.Triangular() in Python
With the help of sympy.stats.Triangular() method, we can get the continuous random variable which represents the triangular distribution. Syntax : sympy.stats.Triangular(name, a, b, c) Where, a, b and c are real number. Return : Return the continuous random variable. Example #1 : In this example we can see that by using sympy.stats.Triangular() met
2 min read
Draw contours on an unstructured triangular grid in Python using Matplotlib
Matplotlib is a Python library which is an open-source drawing library which supports rich drawing types, it can be used to draw 2D and 3D graphics. Data can be understood easily by visualizing it with the help of matplotlib. You can also generate plots, pie charts, histograms, and many other charts as well. It provides a MATLAB-like plotting inter
5 min read
Python Program to check if matrix is lower triangular
Given a square matrix and the task is to check the matrix is in lower triangular form or not. A square matrix is called lower triangular if all the entries above the main diagonal are zero. Examples: Input : mat[4][4] = {{1, 0, 0, 0}, {1, 4, 0, 0}, {4, 6, 2, 0}, {0, 4, 7, 6}};Output : Matrix is in lower triangular form.Input : mat[4][4] = {{1, 0, 0
2 min read
Article Tags :
Practice Tags :