Python中的 numpy.random.triangular()
借助numpy.random.triangular()方法,我们可以从区间[left, right]中获取三角分布的随机样本,并使用该方法返回随机样本。
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.
示例 #1:
在这个例子中我们可以看到,通过使用numpy.random.triangular()方法,我们能够得到三角分布的随机样本并返回 numpy 数组。
Python3
# import numpy
import numpy as np
import matplotlib.pyplot as plt
# Using triangular() method
gfg = np.random.triangular(-5, 0, 5, 5000)
plt.hist(gfg, bins = 50, density = True)
plt.show()
Python3
# import numpy
import numpy as np
import matplotlib.pyplot as plt
# Using triangular() method
gfg = np.random.triangular(-10, 8, 10, 15000)
plt.hist(gfg, bins = 100, density = True)
plt.show()
输出 :
示例 #2:
Python3
# import numpy
import numpy as np
import matplotlib.pyplot as plt
# Using triangular() method
gfg = np.random.triangular(-10, 8, 10, 15000)
plt.hist(gfg, bins = 100, density = True)
plt.show()
输出 :