@@ -492,7 +492,14 @@ def choices(self, population, weights=None, *, cum_weights=None, k=1):
492
492
## -------------------- real-valued distributions -------------------
493
493
494
494
def uniform (self , a , b ):
495
- "Get a random number in the range [a, b) or [a, b] depending on rounding."
495
+ """Get a random number in the range [a, b) or [a, b] depending on rounding.
496
+
497
+ The mean (expected value) and variance of the random variable are:
498
+
499
+ E[X] = (a + b) / 2
500
+ Var[X] = (b - a) ** 2 / 12
501
+
502
+ """
496
503
return a + (b - a ) * self .random ()
497
504
498
505
def triangular (self , low = 0.0 , high = 1.0 , mode = None ):
@@ -503,6 +510,11 @@ def triangular(self, low=0.0, high=1.0, mode=None):
503
510
504
511
http://en.wikipedia.org/wiki/Triangular_distribution
505
512
513
+ The mean (expected value) and variance of the random variable are:
514
+
515
+ E[X] = (low + high + mode) / 3
516
+ Var[X] = (low**2 + high**2 + mode**2 - low*high - low*mode - high*mode) / 18
517
+
506
518
"""
507
519
u = self .random ()
508
520
try :
@@ -593,12 +605,15 @@ def expovariate(self, lambd=1.0):
593
605
positive infinity if lambd is positive, and from negative
594
606
infinity to 0 if lambd is negative.
595
607
596
- """
597
- # lambd: rate lambd = 1/mean
598
- # ('lambda' is a Python reserved word)
608
+ The mean (expected value) and variance of the random variable are:
609
+
610
+ E[X] = 1 / lambd
611
+ Var[X] = 1 / lambd ** 2
599
612
613
+ """
600
614
# we use 1-random() instead of random() to preclude the
601
615
# possibility of taking the log of zero.
616
+
602
617
return - _log (1.0 - self .random ()) / lambd
603
618
604
619
def vonmisesvariate (self , mu , kappa ):
@@ -654,8 +669,12 @@ def gammavariate(self, alpha, beta):
654
669
pdf(x) = --------------------------------------
655
670
math.gamma(alpha) * beta ** alpha
656
671
672
+ The mean (expected value) and variance of the random variable are:
673
+
674
+ E[X] = alpha * beta
675
+ Var[X] = alpha * beta ** 2
676
+
657
677
"""
658
- # alpha > 0, beta > 0, mean is alpha*beta, variance is alpha*beta**2
659
678
660
679
# Warning: a few older sources define the gamma distribution in terms
661
680
# of alpha > -1.0
@@ -714,6 +733,11 @@ def betavariate(self, alpha, beta):
714
733
Conditions on the parameters are alpha > 0 and beta > 0.
715
734
Returned values range between 0 and 1.
716
735
736
+ The mean (expected value) and variance of the random variable are:
737
+
738
+ E[X] = alpha / (alpha + beta)
739
+ Var[X] = alpha * beta / ((alpha + beta)**2 * (alpha + beta + 1))
740
+
717
741
"""
718
742
## See
719
743
## http://mail.python.org/pipermail/python-bugs-list/2001-January/003752.html
@@ -766,6 +790,11 @@ def binomialvariate(self, n=1, p=0.5):
766
790
767
791
Returns an integer in the range: 0 <= X <= n
768
792
793
+ The mean (expected value) and variance of the random variable are:
794
+
795
+ E[X] = n * p
796
+ Var[x] = n * p * (1 - p)
797
+
769
798
"""
770
799
# Error check inputs and handle edge cases
771
800
if n < 0 :
0 commit comments