home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 14 / hacker14.iso / programacao / pythonwin / python.exe / RANDOM.PY < prev    next >
Encoding:
Python Source  |  2003-09-08  |  28.1 KB  |  811 lines

  1. """Random variable generators.
  2.  
  3.     integers
  4.     --------
  5.            uniform within range
  6.  
  7.     sequences
  8.     ---------
  9.            pick random element
  10.            pick random sample
  11.            generate random permutation
  12.  
  13.     distributions on the real line:
  14.     ------------------------------
  15.            uniform
  16.            normal (Gaussian)
  17.            lognormal
  18.            negative exponential
  19.            gamma
  20.            beta
  21.            pareto
  22.            Weibull
  23.  
  24.     distributions on the circle (angles 0 to 2pi)
  25.     ---------------------------------------------
  26.            circular uniform
  27.            von Mises
  28.  
  29. General notes on the underlying Mersenne Twister core generator:
  30.  
  31. * The period is 2**19937-1.
  32. * It is one of the most extensively tested generators in existence
  33. * Without a direct way to compute N steps forward, the
  34.   semantics of jumpahead(n) are weakened to simply jump
  35.   to another distant state and rely on the large period
  36.   to avoid overlapping sequences.
  37. * The random() method is implemented in C, executes in
  38.   a single Python step, and is, therefore, threadsafe.
  39.  
  40. """
  41.  
  42. from math import log as _log, exp as _exp, pi as _pi, e as _e
  43. from math import sqrt as _sqrt, acos as _acos, cos as _cos, sin as _sin
  44. from math import floor as _floor
  45.  
  46. __all__ = ["Random","seed","random","uniform","randint","choice","sample",
  47.            "randrange","shuffle","normalvariate","lognormvariate",
  48.            "cunifvariate","expovariate","vonmisesvariate","gammavariate",
  49.            "stdgamma","gauss","betavariate","paretovariate","weibullvariate",
  50.            "getstate","setstate","jumpahead"]
  51.  
  52. NV_MAGICCONST = 4 * _exp(-0.5)/_sqrt(2.0)
  53. TWOPI = 2.0*_pi
  54. LOG4 = _log(4.0)
  55. SG_MAGICCONST = 1.0 + _log(4.5)
  56.  
  57. # Translated by Guido van Rossum from C source provided by
  58. # Adrian Baddeley.  Adapted by Raymond Hettinger for use with
  59. # the Mersenne Twister core generator.
  60.  
  61. import _random
  62.  
  63. class Random(_random.Random):
  64.     """Random number generator base class used by bound module functions.
  65.  
  66.     Used to instantiate instances of Random to get generators that don't
  67.     share state.  Especially useful for multi-threaded programs, creating
  68.     a different instance of Random for each thread, and using the jumpahead()
  69.     method to ensure that the generated sequences seen by each thread don't
  70.     overlap.
  71.  
  72.     Class Random can also be subclassed if you want to use a different basic
  73.     generator of your own devising: in that case, override the following
  74.     methods:  random(), seed(), getstate(), setstate() and jumpahead().
  75.  
  76.     """
  77.  
  78.     VERSION = 2     # used by getstate/setstate
  79.  
  80.     def __init__(self, x=None):
  81.         """Initialize an instance.
  82.  
  83.         Optional argument x controls seeding, as for Random.seed().
  84.         """
  85.  
  86.         self.seed(x)
  87.         self.gauss_next = None
  88.  
  89.     def seed(self, a=None):
  90.         """Initialize internal state from hashable object.
  91.  
  92.         None or no argument seeds from current time.
  93.  
  94.         If a is not None or an int or long, hash(a) is used instead.
  95.         """
  96.  
  97.         if a is None:
  98.             import time
  99.             a = long(time.time() * 256) # use fractional seconds
  100.         super(Random, self).seed(a)
  101.         self.gauss_next = None
  102.  
  103.     def getstate(self):
  104.         """Return internal state; can be passed to setstate() later."""
  105.         return self.VERSION, super(Random, self).getstate(), self.gauss_next
  106.  
  107.     def setstate(self, state):
  108.         """Restore internal state from object returned by getstate()."""
  109.         version = state[0]
  110.         if version == 2:
  111.             version, internalstate, self.gauss_next = state
  112.             super(Random, self).setstate(internalstate)
  113.         else:
  114.             raise ValueError("state with version %s passed to "
  115.                              "Random.setstate() of version %s" %
  116.                              (version, self.VERSION))
  117.  
  118. ## ---- Methods below this point do not need to be overridden when
  119. ## ---- subclassing for the purpose of using a different core generator.
  120.  
  121. ## -------------------- pickle support  -------------------
  122.  
  123.     def __getstate__(self): # for pickle
  124.         return self.getstate()
  125.  
  126.     def __setstate__(self, state):  # for pickle
  127.         self.setstate(state)
  128.  
  129.     def __reduce__(self):
  130.         return self.__class__, (), self.getstate()
  131.  
  132. ## -------------------- integer methods  -------------------
  133.  
  134.     def randrange(self, start, stop=None, step=1, int=int, default=None):
  135.         """Choose a random item from range(start, stop[, step]).
  136.  
  137.         This fixes the problem with randint() which includes the
  138.         endpoint; in Python this is usually not what you want.
  139.         Do not supply the 'int' and 'default' arguments.
  140.         """
  141.  
  142.         # This code is a bit messy to make it fast for the
  143.         # common case while still doing adequate error checking.
  144.         istart = int(start)
  145.         if istart != start:
  146.             raise ValueError, "non-integer arg 1 for randrange()"
  147.         if stop is default:
  148.             if istart > 0:
  149.                 return int(self.random() * istart)
  150.             raise ValueError, "empty range for randrange()"
  151.  
  152.         # stop argument supplied.
  153.         istop = int(stop)
  154.         if istop != stop:
  155.             raise ValueError, "non-integer stop for randrange()"
  156.         if step == 1 and istart < istop:
  157.             # Note that
  158.             #     int(istart + self.random()*(istop - istart))
  159.             # instead would be incorrect.  For example, consider istart
  160.             # = -2 and istop = 0.  Then the guts would be in
  161.             # -2.0 to 0.0 exclusive on both ends (ignoring that random()
  162.             # might return 0.0), and because int() truncates toward 0, the
  163.             # final result would be -1 or 0 (instead of -2 or -1).
  164.             #     istart + int(self.random()*(istop - istart))
  165.             # would also be incorrect, for a subtler reason:  the RHS
  166.             # can return a long, and then randrange() would also return
  167.             # a long, but we're supposed to return an int (for backward
  168.             # compatibility).
  169.             return int(istart + int(self.random()*(istop - istart)))
  170.         if step == 1:
  171.             raise ValueError, "empty range for randrange()"
  172.  
  173.         # Non-unit step argument supplied.
  174.         istep = int(step)
  175.         if istep != step:
  176.             raise ValueError, "non-integer step for randrange()"
  177.         if istep > 0:
  178.             n = (istop - istart + istep - 1) / istep
  179.         elif istep < 0:
  180.             n = (istop - istart + istep + 1) / istep
  181.         else:
  182.             raise ValueError, "zero step for randrange()"
  183.  
  184.         if n <= 0:
  185.             raise ValueError, "empty range for randrange()"
  186.         return istart + istep*int(self.random() * n)
  187.  
  188.     def randint(self, a, b):
  189.         """Return random integer in range [a, b], including both end points.
  190.         """
  191.  
  192.         return self.randrange(a, b+1)
  193.  
  194. ## -------------------- sequence methods  -------------------
  195.  
  196.     def choice(self, seq):
  197.         """Choose a random element from a non-empty sequence."""
  198.         return seq[int(self.random() * len(seq))]
  199.  
  200.     def shuffle(self, x, random=None, int=int):
  201.         """x, random=random.random -> shuffle list x in place; return None.
  202.  
  203.         Optional arg random is a 0-argument function returning a random
  204.         float in [0.0, 1.0); by default, the standard random.random.
  205.  
  206.         Note that for even rather small len(x), the total number of
  207.         permutations of x is larger than the period of most random number
  208.         generators; this implies that "most" permutations of a long
  209.         sequence can never be generated.
  210.         """
  211.  
  212.         if random is None:
  213.             random = self.random
  214.         for i in xrange(len(x)-1, 0, -1):
  215.             # pick an element in x[:i+1] with which to exchange x[i]
  216.             j = int(random() * (i+1))
  217.             x[i], x[j] = x[j], x[i]
  218.  
  219.     def sample(self, population, k):
  220.         """Chooses k unique random elements from a population sequence.
  221.  
  222.         Returns a new list containing elements from the population while
  223.         leaving the original population unchanged.  The resulting list is
  224.         in selection order so that all sub-slices will also be valid random
  225.         samples.  This allows raffle winners (the sample) to be partitioned
  226.         into grand prize and second place winners (the subslices).
  227.  
  228.         Members of the population need not be hashable or unique.  If the
  229.         population contains repeats, then each occurrence is a possible
  230.         selection in the sample.
  231.  
  232.         To choose a sample in a range of integers, use xrange as an argument.
  233.         This is especially fast and space efficient for sampling from a
  234.         large population:   sample(xrange(10000000), 60)
  235.         """
  236.  
  237.         # Sampling without replacement entails tracking either potential
  238.         # selections (the pool) in a list or previous selections in a
  239.         # dictionary.
  240.  
  241.         # When the number of selections is small compared to the population,
  242.         # then tracking selections is efficient, requiring only a small
  243.         # dictionary and an occasional reselection.  For a larger number of
  244.         # selections, the pool tracking method is preferred since the list takes
  245.         # less space than the dictionary and it doesn't suffer from frequent
  246.         # reselections.
  247.  
  248.         n = len(population)
  249.         if not 0 <= k <= n:
  250.             raise ValueError, "sample larger than population"
  251.         random = self.random
  252.         _int = int
  253.         result = [None] * k
  254.         if n < 6 * k:     # if n len list takes less space than a k len dict
  255.             pool = list(population)
  256.             for i in xrange(k):         # invariant:  non-selected at [0,n-i)
  257.                 j = _int(random() * (n-i))
  258.                 result[i] = pool[j]
  259.                 pool[j] = pool[n-i-1]   # move non-selected item into vacancy
  260.         else:
  261.             try:
  262.                 n > 0 and (population[0], population[n//2], population[n-1])
  263.             except (TypeError, KeyError):   # handle sets and dictionaries
  264.                 population = tuple(population)
  265.             selected = {}
  266.             for i in xrange(k):
  267.                 j = _int(random() * n)
  268.                 while j in selected:
  269.                     j = _int(random() * n)
  270.                 result[i] = selected[j] = population[j]
  271.         return result
  272.  
  273. ## -------------------- real-valued distributions  -------------------
  274.  
  275. ## -------------------- uniform distribution -------------------
  276.  
  277.     def uniform(self, a, b):
  278.         """Get a random number in the range [a, b)."""
  279.         return a + (b-a) * self.random()
  280.  
  281. ## -------------------- normal distribution --------------------
  282.  
  283.     def normalvariate(self, mu, sigma):
  284.         """Normal distribution.
  285.  
  286.         mu is the mean, and sigma is the standard deviation.
  287.  
  288.         """
  289.         # mu = mean, sigma = standard deviation
  290.  
  291.         # Uses Kinderman and Monahan method. Reference: Kinderman,
  292.         # A.J. and Monahan, J.F., "Computer generation of random
  293.         # variables using the ratio of uniform deviates", ACM Trans
  294.         # Math Software, 3, (1977), pp257-260.
  295.  
  296.         random = self.random
  297.         while True:
  298.             u1 = random()
  299.             u2 = 1.0 - random()
  300.             z = NV_MAGICCONST*(u1-0.5)/u2
  301.             zz = z*z/4.0
  302.             if zz <= -_log(u2):
  303.                 break
  304.         return mu + z*sigma
  305.  
  306. ## -------------------- lognormal distribution --------------------
  307.  
  308.     def lognormvariate(self, mu, sigma):
  309.         """Log normal distribution.
  310.  
  311.         If you take the natural logarithm of this distribution, you'll get a
  312.         normal distribution with mean mu and standard deviation sigma.
  313.         mu can have any value, and sigma must be greater than zero.
  314.  
  315.         """
  316.         return _exp(self.normalvariate(mu, sigma))
  317.  
  318. ## -------------------- circular uniform --------------------
  319.  
  320.     def cunifvariate(self, mean, arc):
  321.         """Circular uniform distribution.
  322.  
  323.         mean is the mean angle, and arc is the range of the distribution,
  324.         centered around the mean angle.  Both values must be expressed in
  325.         radians.  Returned values range between mean - arc/2 and
  326.         mean + arc/2 and are normalized to between 0 and pi.
  327.  
  328.         Deprecated in version 2.3.  Use:
  329.             (mean + arc * (Random.random() - 0.5)) % Math.pi
  330.  
  331.         """
  332.         # mean: mean angle (in radians between 0 and pi)
  333.         # arc:  range of distribution (in radians between 0 and pi)
  334.         import warnings
  335.         warnings.warn("The cunifvariate function is deprecated; Use (mean "
  336.                       "+ arc * (Random.random() - 0.5)) % Math.pi instead.",
  337.                       DeprecationWarning, 2)
  338.  
  339.         return (mean + arc * (self.random() - 0.5)) % _pi
  340.  
  341. ## -------------------- exponential distribution --------------------
  342.  
  343.     def expovariate(self, lambd):
  344.         """Exponential distribution.
  345.  
  346.         lambd is 1.0 divided by the desired mean.  (The parameter would be
  347.         called "lambda", but that is a reserved word in Python.)  Returned
  348.         values range from 0 to positive infinity.
  349.  
  350.         """
  351.         # lambd: rate lambd = 1/mean
  352.         # ('lambda' is a Python reserved word)
  353.  
  354.         random = self.random
  355.         u = random()
  356.         while u <= 1e-7:
  357.             u = random()
  358.         return -_log(u)/lambd
  359.  
  360. ## -------------------- von Mises distribution --------------------
  361.  
  362.     def vonmisesvariate(self, mu, kappa):
  363.         """Circular data distribution.
  364.  
  365.         mu is the mean angle, expressed in radians between 0 and 2*pi, and
  366.         kappa is the concentration parameter, which must be greater than or
  367.         equal to zero.  If kappa is equal to zero, this distribution reduces
  368.         to a uniform random angle over the range 0 to 2*pi.
  369.  
  370.         """
  371.         # mu:    mean angle (in radians between 0 and 2*pi)
  372.         # kappa: concentration parameter kappa (>= 0)
  373.         # if kappa = 0 generate uniform random angle
  374.  
  375.         # Based upon an algorithm published in: Fisher, N.I.,
  376.         # "Statistical Analysis of Circular Data", Cambridge
  377.         # University Press, 1993.
  378.  
  379.         # Thanks to Magnus Kessler for a correction to the
  380.         # implementation of step 4.
  381.  
  382.         random = self.random
  383.         if kappa <= 1e-6:
  384.             return TWOPI * random()
  385.  
  386.         a = 1.0 + _sqrt(1.0 + 4.0 * kappa * kappa)
  387.         b = (a - _sqrt(2.0 * a))/(2.0 * kappa)
  388.         r = (1.0 + b * b)/(2.0 * b)
  389.  
  390.         while True:
  391.             u1 = random()
  392.  
  393.             z = _cos(_pi * u1)
  394.             f = (1.0 + r * z)/(r + z)
  395.             c = kappa * (r - f)
  396.  
  397.             u2 = random()
  398.  
  399.             if not (u2 >= c * (2.0 - c) and u2 > c * _exp(1.0 - c)):
  400.                 break
  401.  
  402.         u3 = random()
  403.         if u3 > 0.5:
  404.             theta = (mu % TWOPI) + _acos(f)
  405.         else:
  406.             theta = (mu % TWOPI) - _acos(f)
  407.  
  408.         return theta
  409.  
  410. ## -------------------- gamma distribution --------------------
  411.  
  412.     def gammavariate(self, alpha, beta):
  413.         """Gamma distribution.  Not the gamma function!
  414.  
  415.         Conditions on the parameters are alpha > 0 and beta > 0.
  416.  
  417.         """
  418.  
  419.         # alpha > 0, beta > 0, mean is alpha*beta, variance is alpha*beta**2
  420.  
  421.         # Warning: a few older sources define the gamma distribution in terms
  422.         # of alpha > -1.0
  423.         if alpha <= 0.0 or beta <= 0.0:
  424.             raise ValueError, 'gammavariate: alpha and beta must be > 0.0'
  425.  
  426.         random = self.random
  427.         if alpha > 1.0:
  428.  
  429.             # Uses R.C.H. Cheng, "The generation of Gamma
  430.             # variables with non-integral shape parameters",
  431.             # Applied Statistics, (1977), 26, No. 1, p71-74
  432.  
  433.             ainv = _sqrt(2.0 * alpha - 1.0)
  434.             bbb = alpha - LOG4
  435.             ccc = alpha + ainv
  436.  
  437.             while True:
  438.                 u1 = random()
  439.                 if not 1e-7 < u1 < .9999999:
  440.                     continue
  441.                 u2 = 1.0 - random()
  442.                 v = _log(u1/(1.0-u1))/ainv
  443.                 x = alpha*_exp(v)
  444.                 z = u1*u1*u2
  445.                 r = bbb+ccc*v-x
  446.                 if r + SG_MAGICCONST - 4.5*z >= 0.0 or r >= _log(z):
  447.                     return x * beta
  448.  
  449.         elif alpha == 1.0:
  450.             # expovariate(1)
  451.             u = random()
  452.             while u <= 1e-7:
  453.                 u = random()
  454.             return -_log(u) * beta
  455.  
  456.         else:   # alpha is between 0 and 1 (exclusive)
  457.  
  458.             # Uses ALGORITHM GS of Statistical Computing - Kennedy & Gentle
  459.  
  460.             while True:
  461.                 u = random()
  462.                 b = (_e + alpha)/_e
  463.                 p = b*u
  464.                 if p <= 1.0:
  465.                     x = pow(p, 1.0/alpha)
  466.                 else:
  467.                     # p > 1
  468.                     x = -_log((b-p)/alpha)
  469.                 u1 = random()
  470.                 if not (((p <= 1.0) and (u1 > _exp(-x))) or
  471.                           ((p > 1)  and  (u1 > pow(x, alpha - 1.0)))):
  472.                     break
  473.             return x * beta
  474.  
  475.  
  476.     def stdgamma(self, alpha, ainv, bbb, ccc):
  477.         # This method was (and shall remain) undocumented.
  478.         # This method is deprecated
  479.         # for the following reasons:
  480.         # 1. Returns same as .gammavariate(alpha, 1.0)
  481.         # 2. Requires caller to provide 3 extra arguments
  482.         #    that are functions of alpha anyway
  483.         # 3. Can't be used for alpha < 0.5
  484.  
  485.         # ainv = sqrt(2 * alpha - 1)
  486.         # bbb = alpha - log(4)
  487.         # ccc = alpha + ainv
  488.         import warnings
  489.         warnings.warn("The stdgamma function is deprecated; "
  490.                       "use gammavariate() instead.",
  491.                       DeprecationWarning, 2)
  492.         return self.gammavariate(alpha, 1.0)
  493.  
  494.  
  495.  
  496. ## -------------------- Gauss (faster alternative) --------------------
  497.  
  498.     def gauss(self, mu, sigma):
  499.         """Gaussian distribution.
  500.  
  501.         mu is the mean, and sigma is the standard deviation.  This is
  502.         slightly faster than the normalvariate() function.
  503.  
  504.         Not thread-safe without a lock around calls.
  505.  
  506.         """
  507.  
  508.         # When x and y are two variables from [0, 1), uniformly
  509.         # distributed, then
  510.         #
  511.         #    cos(2*pi*x)*sqrt(-2*log(1-y))
  512.         #    sin(2*pi*x)*sqrt(-2*log(1-y))
  513.         #
  514.         # are two *independent* variables with normal distribution
  515.         # (mu = 0, sigma = 1).
  516.         # (Lambert Meertens)
  517.         # (corrected version; bug discovered by Mike Miller, fixed by LM)
  518.  
  519.         # Multithreading note: When two threads call this function
  520.         # simultaneously, it is possible that they will receive the
  521.         # same return value.  The window is very small though.  To
  522.         # avoid this, you have to use a lock around all calls.  (I
  523.         # didn't want to slow this down in the serial case by using a
  524.         # lock here.)
  525.  
  526.         random = self.random
  527.         z = self.gauss_next
  528.         self.gauss_next = None
  529.         if z is None:
  530.             x2pi = random() * TWOPI
  531.             g2rad = _sqrt(-2.0 * _log(1.0 - random()))
  532.             z = _cos(x2pi) * g2rad
  533.             self.gauss_next = _sin(x2pi) * g2rad
  534.  
  535.         return mu + z*sigma
  536.  
  537. ## -------------------- beta --------------------
  538. ## See
  539. ## http://sourceforge.net/bugs/?func=detailbug&bug_id=130030&group_id=5470
  540. ## for Ivan Frohne's insightful analysis of why the original implementation:
  541. ##
  542. ##    def betavariate(self, alpha, beta):
  543. ##        # Discrete Event Simulation in C, pp 87-88.
  544. ##
  545. ##        y = self.expovariate(alpha)
  546. ##        z = self.expovariate(1.0/beta)
  547. ##        return z/(y+z)
  548. ##
  549. ## was dead wrong, and how it probably got that way.
  550.  
  551.     def betavariate(self, alpha, beta):
  552.         """Beta distribution.
  553.  
  554.         Conditions on the parameters are alpha > -1 and beta} > -1.
  555.         Returned values range between 0 and 1.
  556.  
  557.         """
  558.  
  559.         # This version due to Janne Sinkkonen, and matches all the std
  560.         # texts (e.g., Knuth Vol 2 Ed 3 pg 134 "the beta distribution").
  561.         y = self.gammavariate(alpha, 1.)
  562.         if y == 0:
  563.             return 0.0
  564.         else:
  565.             return y / (y + self.gammavariate(beta, 1.))
  566.  
  567. ## -------------------- Pareto --------------------
  568.  
  569.     def paretovariate(self, alpha):
  570.         """Pareto distribution.  alpha is the shape parameter."""
  571.         # Jain, pg. 495
  572.  
  573.         u = 1.0 - self.random()
  574.         return 1.0 / pow(u, 1.0/alpha)
  575.  
  576. ## -------------------- Weibull --------------------
  577.  
  578.     def weibullvariate(self, alpha, beta):
  579.         """Weibull distribution.
  580.  
  581.         alpha is the scale parameter and beta is the shape parameter.
  582.  
  583.         """
  584.         # Jain, pg. 499; bug fix courtesy Bill Arms
  585.  
  586.         u = 1.0 - self.random()
  587.         return alpha * pow(-_log(u), 1.0/beta)
  588.  
  589. ## -------------------- Wichmann-Hill -------------------
  590.  
  591. class WichmannHill(Random):
  592.  
  593.     VERSION = 1     # used by getstate/setstate
  594.  
  595.     def seed(self, a=None):
  596.         """Initialize internal state from hashable object.
  597.  
  598.         None or no argument seeds from current time.
  599.  
  600.         If a is not None or an int or long, hash(a) is used instead.
  601.  
  602.         If a is an int or long, a is used directly.  Distinct values between
  603.         0 and 27814431486575L inclusive are guaranteed to yield distinct
  604.         internal states (this guarantee is specific to the default
  605.         Wichmann-Hill generator).
  606.         """
  607.  
  608.         if a is None:
  609.             # Initialize from current time
  610.             import time
  611.             a = long(time.time() * 256)
  612.  
  613.         if not isinstance(a, (int, long)):
  614.             a = hash(a)
  615.  
  616.         a, x = divmod(a, 30268)
  617.         a, y = divmod(a, 30306)
  618.         a, z = divmod(a, 30322)
  619.         self._seed = int(x)+1, int(y)+1, int(z)+1
  620.  
  621.         self.gauss_next = None
  622.  
  623.     def random(self):
  624.         """Get the next random number in the range [0.0, 1.0)."""
  625.  
  626.         # Wichman-Hill random number generator.
  627.         #
  628.         # Wichmann, B. A. & Hill, I. D. (1982)
  629.         # Algorithm AS 183:
  630.         # An efficient and portable pseudo-random number generator
  631.         # Applied Statistics 31 (1982) 188-190
  632.         #
  633.         # see also:
  634.         #        Correction to Algorithm AS 183
  635.         #        Applied Statistics 33 (1984) 123
  636.         #
  637.         #        McLeod, A. I. (1985)
  638.         #        A remark on Algorithm AS 183
  639.         #        Applied Statistics 34 (1985),198-200
  640.  
  641.         # This part is thread-unsafe:
  642.         # BEGIN CRITICAL SECTION
  643.         x, y, z = self._seed
  644.         x = (171 * x) % 30269
  645.         y = (172 * y) % 30307
  646.         z = (170 * z) % 30323
  647.         self._seed = x, y, z
  648.         # END CRITICAL SECTION
  649.  
  650.         # Note:  on a platform using IEEE-754 double arithmetic, this can
  651.         # never return 0.0 (asserted by Tim; proof too long for a comment).
  652.         return (x/30269.0 + y/30307.0 + z/30323.0) % 1.0
  653.  
  654.     def getstate(self):
  655.         """Return internal state; can be passed to setstate() later."""
  656.         return self.VERSION, self._seed, self.gauss_next
  657.  
  658.     def setstate(self, state):
  659.         """Restore internal state from object returned by getstate()."""
  660.         version = state[0]
  661.         if version == 1:
  662.             version, self._seed, self.gauss_next = state
  663.         else:
  664.             raise ValueError("state with version %s passed to "
  665.                              "Random.setstate() of version %s" %
  666.                              (version, self.VERSION))
  667.  
  668.     def jumpahead(self, n):
  669.         """Act as if n calls to random() were made, but quickly.
  670.  
  671.         n is an int, greater than or equal to 0.
  672.  
  673.         Example use:  If you have 2 threads and know that each will
  674.         consume no more than a million random numbers, create two Random
  675.         objects r1 and r2, then do
  676.             r2.setstate(r1.getstate())
  677.             r2.jumpahead(1000000)
  678.         Then r1 and r2 will use guaranteed-disjoint segments of the full
  679.         period.
  680.         """
  681.  
  682.         if not n >= 0:
  683.             raise ValueError("n must be >= 0")
  684.         x, y, z = self._seed
  685.         x = int(x * pow(171, n, 30269)) % 30269
  686.         y = int(y * pow(172, n, 30307)) % 30307
  687.         z = int(z * pow(170, n, 30323)) % 30323
  688.         self._seed = x, y, z
  689.  
  690.     def __whseed(self, x=0, y=0, z=0):
  691.         """Set the Wichmann-Hill seed from (x, y, z).
  692.  
  693.         These must be integers in the range [0, 256).
  694.         """
  695.  
  696.         if not type(x) == type(y) == type(z) == int:
  697.             raise TypeError('seeds must be integers')
  698.         if not (0 <= x < 256 and 0 <= y < 256 and 0 <= z < 256):
  699.             raise ValueError('seeds must be in range(0, 256)')
  700.         if 0 == x == y == z:
  701.             # Initialize from current time
  702.             import time
  703.             t = long(time.time() * 256)
  704.             t = int((t&0xffffff) ^ (t>>24))
  705.             t, x = divmod(t, 256)
  706.             t, y = divmod(t, 256)
  707.             t, z = divmod(t, 256)
  708.         # Zero is a poor seed, so substitute 1
  709.         self._seed = (x or 1, y or 1, z or 1)
  710.  
  711.         self.gauss_next = None
  712.  
  713.     def whseed(self, a=None):
  714.         """Seed from hashable object's hash code.
  715.  
  716.         None or no argument seeds from current time.  It is not guaranteed
  717.         that objects with distinct hash codes lead to distinct internal
  718.         states.
  719.  
  720.         This is obsolete, provided for compatibility with the seed routine
  721.         used prior to Python 2.1.  Use the .seed() method instead.
  722.         """
  723.  
  724.         if a is None:
  725.             self.__whseed()
  726.             return
  727.         a = hash(a)
  728.         a, x = divmod(a, 256)
  729.         a, y = divmod(a, 256)
  730.         a, z = divmod(a, 256)
  731.         x = (x + a) % 256 or 1
  732.         y = (y + a) % 256 or 1
  733.         z = (z + a) % 256 or 1
  734.         self.__whseed(x, y, z)
  735.  
  736. ## -------------------- test program --------------------
  737.  
  738. def _test_generator(n, funccall):
  739.     import time
  740.     print n, 'times', funccall
  741.     code = compile(funccall, funccall, 'eval')
  742.     total = 0.0
  743.     sqsum = 0.0
  744.     smallest = 1e10
  745.     largest = -1e10
  746.     t0 = time.time()
  747.     for i in range(n):
  748.         x = eval(code)
  749.         total += x
  750.         sqsum = sqsum + x*x
  751.         smallest = min(x, smallest)
  752.         largest = max(x, largest)
  753.     t1 = time.time()
  754.     print round(t1-t0, 3), 'sec,',
  755.     avg = total/n
  756.     stddev = _sqrt(sqsum/n - avg*avg)
  757.     print 'avg %g, stddev %g, min %g, max %g' % \
  758.               (avg, stddev, smallest, largest)
  759.  
  760.  
  761. def _test(N=2000):
  762.     _test_generator(N, 'random()')
  763.     _test_generator(N, 'normalvariate(0.0, 1.0)')
  764.     _test_generator(N, 'lognormvariate(0.0, 1.0)')
  765.     _test_generator(N, 'cunifvariate(0.0, 1.0)')
  766.     _test_generator(N, 'vonmisesvariate(0.0, 1.0)')
  767.     _test_generator(N, 'gammavariate(0.01, 1.0)')
  768.     _test_generator(N, 'gammavariate(0.1, 1.0)')
  769.     _test_generator(N, 'gammavariate(0.1, 2.0)')
  770.     _test_generator(N, 'gammavariate(0.5, 1.0)')
  771.     _test_generator(N, 'gammavariate(0.9, 1.0)')
  772.     _test_generator(N, 'gammavariate(1.0, 1.0)')
  773.     _test_generator(N, 'gammavariate(2.0, 1.0)')
  774.     _test_generator(N, 'gammavariate(20.0, 1.0)')
  775.     _test_generator(N, 'gammavariate(200.0, 1.0)')
  776.     _test_generator(N, 'gauss(0.0, 1.0)')
  777.     _test_generator(N, 'betavariate(3.0, 3.0)')
  778.  
  779. # Create one instance, seeded from current time, and export its methods
  780. # as module-level functions.  The functions share state across all uses
  781. #(both in the user's code and in the Python libraries), but that's fine
  782. # for most programs and is easier for the casual user than making them
  783. # instantiate their own Random() instance.
  784.  
  785. _inst = Random()
  786. seed = _inst.seed
  787. random = _inst.random
  788. uniform = _inst.uniform
  789. randint = _inst.randint
  790. choice = _inst.choice
  791. randrange = _inst.randrange
  792. sample = _inst.sample
  793. shuffle = _inst.shuffle
  794. normalvariate = _inst.normalvariate
  795. lognormvariate = _inst.lognormvariate
  796. cunifvariate = _inst.cunifvariate
  797. expovariate = _inst.expovariate
  798. vonmisesvariate = _inst.vonmisesvariate
  799. gammavariate = _inst.gammavariate
  800. stdgamma = _inst.stdgamma
  801. gauss = _inst.gauss
  802. betavariate = _inst.betavariate
  803. paretovariate = _inst.paretovariate
  804. weibullvariate = _inst.weibullvariate
  805. getstate = _inst.getstate
  806. setstate = _inst.setstate
  807. jumpahead = _inst.jumpahead
  808.  
  809. if __name__ == '__main__':
  810.     _test()
  811.