MCPcopy Index your code
hub / github.com/python/cpython / triangular

Method triangular

Lib/random.py:511–534  ·  view source on GitHub ↗

Triangular distribution. Continuous distribution bounded by given lower and upper limits, and having a given mode value in-between. http://en.wikipedia.org/wiki/Triangular_distribution The mean (expected value) and variance of the random variable are:

(self, low=0.0, high=1.0, mode=None)

Source from the content-addressed store, hash-verified

509 return a + (b - a) * self.random()
510
511 def triangular(self, low=0.0, high=1.0, mode=None):
512 """Triangular distribution.
513
514 Continuous distribution bounded by given lower and upper limits,
515 and having a given mode value in-between.
516
517 http://en.wikipedia.org/wiki/Triangular_distribution
518
519 The mean (expected value) and variance of the random variable are:
520
521 E[X] = (low + high + mode) / 3
522 Var[X] = (low**2 + high**2 + mode**2 - low*high - low*mode - high*mode) / 18
523
524 """
525 u = self.random()
526 try:
527 c = 0.5 if mode is None else (mode - low) / (high - low)
528 except ZeroDivisionError:
529 return low
530 if u > c:
531 u = 1.0 - u
532 c = 1.0 - c
533 low, high = high, low
534 return low + (high - low) * _sqrt(u * c)
535
536 def normalvariate(self, mu=0.0, sigma=1.0):
537 """Normal distribution.

Callers 2

test_zeroinputsMethod · 0.95
test_basicsMethod · 0.80

Calls 1

randomMethod · 0.45

Tested by 2

test_zeroinputsMethod · 0.76
test_basicsMethod · 0.64