Gaussian distribution. mu is the mean, and sigma is the standard deviation. This is slightly faster than the normalvariate() function. Not thread-safe without a lock around calls.
(self, mu=0.0, sigma=1.0)
| 555 | return mu + z * sigma |
| 556 | |
| 557 | def gauss(self, mu=0.0, sigma=1.0): |
| 558 | """Gaussian distribution. |
| 559 | |
| 560 | mu is the mean, and sigma is the standard deviation. This is |
| 561 | slightly faster than the normalvariate() function. |
| 562 | |
| 563 | Not thread-safe without a lock around calls. |
| 564 | |
| 565 | """ |
| 566 | # When x and y are two variables from [0, 1), uniformly |
| 567 | # distributed, then |
| 568 | # |
| 569 | # cos(2*pi*x)*sqrt(-2*log(1-y)) |
| 570 | # sin(2*pi*x)*sqrt(-2*log(1-y)) |
| 571 | # |
| 572 | # are two *independent* variables with normal distribution |
| 573 | # (mu = 0, sigma = 1). |
| 574 | # (Lambert Meertens) |
| 575 | # (corrected version; bug discovered by Mike Miller, fixed by LM) |
| 576 | |
| 577 | # Multithreading note: When two threads call this function |
| 578 | # simultaneously, it is possible that they will receive the |
| 579 | # same return value. The window is very small though. To |
| 580 | # avoid this, you have to use a lock around all calls. (I |
| 581 | # didn't want to slow this down in the serial case by using a |
| 582 | # lock here.) |
| 583 | |
| 584 | random = self.random |
| 585 | z = self.gauss_next |
| 586 | self.gauss_next = None |
| 587 | if z is None: |
| 588 | x2pi = random() * TWOPI |
| 589 | g2rad = _sqrt(-2.0 * _log(1.0 - random())) |
| 590 | z = _cos(x2pi) * g2rad |
| 591 | self.gauss_next = _sin(x2pi) * g2rad |
| 592 | |
| 593 | return mu + z * sigma |
| 594 | |
| 595 | def lognormvariate(self, mu, sigma): |
| 596 | """Log normal distribution. |
no outgoing calls