Python random triangular














































Python random triangular



Python random  :  triangular()


This article demonstrates how to use random.triangular() function . 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(lowhighmode)


Parameter Values :

ParameterDescription
lowOptional. A number specifying the lowest possible outcome. (included)
Default 0
highOptional. A number specifying the highest possible outcome. (included)
Default 1
modeOptional. A number used to weigh the result in any direction.
Default the midpoint between the low and high values


Return Value :

a random floating number

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.


Examples :

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 float number :",random.triangular(low, high, mode)) 

Output :

Random float number : 39.97468598467287


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 :

50.64589765434788
83.24091007054133
57.73256510817383

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:



   *****END OF ARTICLE*****



More Articles of Vishal Lodhi:

Name Views Likes
Python string zfill 114 0
Python string swapcase 92 0
Python string title 97 0
Python string startswith 103 0
Python string replace 144 0
Python string translate 85 0
Python string rpartition 85 0
Python string partition 74 0
Python string splitlines 114 0
Python string rsplit 79 0
Python string split 75 0
Python string rindex 82 0
Python string rfind 106 0
Python string upper 80 0
Python string lower 81 0
Python string maketrans 90 0
Python string strip 81 0
Python string rstrip 91 0
Python string lstrip 79 0
Python string rjust 96 0
Python string ljust 95 0
Python string len 82 0
Python string join 75 0
Python string casefold 82 0
Python string isprintable 85 0
Python string encode 78 0
Python string isdecimal 84 0
Python string isidentifier 78 0
Python string isupper 77 0
Python string istitle 80 0
Python string isspace 100 0
Python string isnumeric 102 0
Python string isdigit 82 0
Python string islower 84 0
Python string isalpha 96 0
Python string isalnum 102 0
Python string index 87 0
Python string find 93 0
Python string expandtabs() 81 0
Python string endswith 91 0
Python string count 86 0
Python string capitalize 78 0
Python string center 84 0
Python string Introduction 98 0
Python string Template 140 0
Python string Formatter 145 0
Python string printable 199 0
Python string whitespace 146 0
Python string punctuation 104 0
Python string octdigits 86 0
Python string hexdigits 104 0
Python string digits 79 0
Python string ascii_letters 344 0
Python string ascii_lowercase 147 0
Python string ascii_uppercase 156 0
How to Create Download Manager in Python 524 0
Python random weibullvariate 164 0
python random paretovariate 133 0
Python random vonmisesvariate 178 0
Python random normalvariate 196 0
Python random lognormvariate 120 0
Python random gauss 120 0
Python random gammavariate 131 0
Python random expovariate 194 0
Python random betavariate 185 0
Python random triangular 117 0
Python random uniform 141 0
Python random random 117 0
Python random sample 182 0
Python random shuffle 178 0
Python random choices 234 0
Python random choice 166 0
Python random randint 148 0
Python random randrange 214 0
Python random getrandbits 141 0
Python random setstate 136 0
Python random getstate 178 0
Python random seed 168 1
Python random Introduction 175 0

Comments