annotate Lib/random.py @ 17322:bd110eadc19a legacy-trunk

One-liner critical fix from Jim Fulton: Eric's string-method crusade got the order backwards in a line (for .find()).
author Tim Peters <tim.peters@gmail.com>
date Thu, 05 Apr 2001 22:26:23 +0000
parents e2a63584566f
children fed5071da957
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11200
diff changeset
1 """Random variable generators.
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11200
diff changeset
2
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
3 integers
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
4 --------
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
5 uniform within range
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
6
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
7 sequences
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
8 ---------
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
9 pick random element
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
10 generate random permutation
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
11
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11200
diff changeset
12 distributions on the real line:
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11200
diff changeset
13 ------------------------------
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
14 uniform
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11200
diff changeset
15 normal (Gaussian)
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11200
diff changeset
16 lognormal
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11200
diff changeset
17 negative exponential
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11200
diff changeset
18 gamma
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11200
diff changeset
19 beta
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
20
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11200
diff changeset
21 distributions on the circle (angles 0 to 2pi)
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11200
diff changeset
22 ---------------------------------------------
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11200
diff changeset
23 circular uniform
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11200
diff changeset
24 von Mises
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
25
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11200
diff changeset
26 Translated from anonymously contributed C/C++ source.
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11200
diff changeset
27
16272
835e9dd54bfb The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents: 16271
diff changeset
28 Multi-threading note: the random number generator used here is not thread-
835e9dd54bfb The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents: 16271
diff changeset
29 safe; it is possible that two calls return the same random value. However,
835e9dd54bfb The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents: 16271
diff changeset
30 you can instantiate a different instance of Random() in each thread to get
835e9dd54bfb The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents: 16271
diff changeset
31 generators that don't share state, then use .setstate() and .jumpahead() to
835e9dd54bfb The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents: 16271
diff changeset
32 move the generators to disjoint segments of the full period. For example,
835e9dd54bfb The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents: 16271
diff changeset
33
835e9dd54bfb The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents: 16271
diff changeset
34 def create_generators(num, delta, firstseed=None):
835e9dd54bfb The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents: 16271
diff changeset
35 ""\"Return list of num distinct generators.
835e9dd54bfb The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents: 16271
diff changeset
36 Each generator has its own unique segment of delta elements from
835e9dd54bfb The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents: 16271
diff changeset
37 Random.random()'s full period.
835e9dd54bfb The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents: 16271
diff changeset
38 Seed the first generator with optional arg firstseed (default is
835e9dd54bfb The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents: 16271
diff changeset
39 None, to seed from current time).
835e9dd54bfb The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents: 16271
diff changeset
40 ""\"
835e9dd54bfb The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents: 16271
diff changeset
41
835e9dd54bfb The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents: 16271
diff changeset
42 from random import Random
835e9dd54bfb The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents: 16271
diff changeset
43 g = Random(firstseed)
835e9dd54bfb The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents: 16271
diff changeset
44 result = [g]
835e9dd54bfb The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents: 16271
diff changeset
45 for i in range(num - 1):
835e9dd54bfb The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents: 16271
diff changeset
46 laststate = g.getstate()
835e9dd54bfb The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents: 16271
diff changeset
47 g = Random()
835e9dd54bfb The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents: 16271
diff changeset
48 g.setstate(laststate)
835e9dd54bfb The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents: 16271
diff changeset
49 g.jumpahead(delta)
835e9dd54bfb The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents: 16271
diff changeset
50 result.append(g)
835e9dd54bfb The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents: 16271
diff changeset
51 return result
835e9dd54bfb The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents: 16271
diff changeset
52
835e9dd54bfb The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents: 16271
diff changeset
53 gens = create_generators(10, 1000000)
835e9dd54bfb The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents: 16271
diff changeset
54
835e9dd54bfb The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents: 16271
diff changeset
55 That creates 10 distinct generators, which can be passed out to 10 distinct
835e9dd54bfb The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents: 16271
diff changeset
56 threads. The generators don't share state so can be called safely in
835e9dd54bfb The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents: 16271
diff changeset
57 parallel. So long as no thread calls its g.random() more than a million
835e9dd54bfb The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents: 16271
diff changeset
58 times (the second argument to create_generators), the sequences seen by
835e9dd54bfb The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents: 16271
diff changeset
59 each thread will not overlap.
835e9dd54bfb The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents: 16271
diff changeset
60
835e9dd54bfb The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents: 16271
diff changeset
61 The period of the underlying Wichmann-Hill generator is 6,953,607,871,644,
835e9dd54bfb The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents: 16271
diff changeset
62 and that limits how far this technique can be pushed.
835e9dd54bfb The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents: 16271
diff changeset
63
835e9dd54bfb The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents: 16271
diff changeset
64 Just for fun, note that since we know the period, .jumpahead() can also be
835e9dd54bfb The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents: 16271
diff changeset
65 used to "move backward in time":
835e9dd54bfb The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents: 16271
diff changeset
66
835e9dd54bfb The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents: 16271
diff changeset
67 >>> g = Random(42) # arbitrary
835e9dd54bfb The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents: 16271
diff changeset
68 >>> g.random()
16379
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
69 0.25420336316883324
16272
835e9dd54bfb The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents: 16271
diff changeset
70 >>> g.jumpahead(6953607871644L - 1) # move *back* one
835e9dd54bfb The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents: 16271
diff changeset
71 >>> g.random()
16379
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
72 0.25420336316883324
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11200
diff changeset
73 """
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
74 # XXX The docstring sucks.
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
75
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
76 from math import log as _log, exp as _exp, pi as _pi, e as _e
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
77 from math import sqrt as _sqrt, acos as _acos, cos as _cos, sin as _sin
8455
af9273b1073a Make gauss() semi-thread-safe. It can still give duplicate results,
Guido van Rossum <guido@python.org>
parents: 8393
diff changeset
78
16613
f68dbffff927 bunch more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 16386
diff changeset
79 __all__ = ["Random","seed","random","uniform","randint","choice",
f68dbffff927 bunch more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 16386
diff changeset
80 "randrange","shuffle","normalvariate","lognormvariate",
f68dbffff927 bunch more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 16386
diff changeset
81 "cunifvariate","expovariate","vonmisesvariate","gammavariate",
f68dbffff927 bunch more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 16386
diff changeset
82 "stdgamma","gauss","betavariate","paretovariate","weibullvariate",
f68dbffff927 bunch more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 16386
diff changeset
83 "getstate","setstate","jumpahead","whseed"]
16616
e2a63584566f Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 16613
diff changeset
84
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
85 def _verify(name, expected):
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
86 computed = eval(name)
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
87 if abs(computed - expected) > 1e-7:
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
88 raise ValueError(
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
89 "computed value for %s deviates too much "
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
90 "(computed %g, expected %g)" % (name, computed, expected))
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
91
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
92 NV_MAGICCONST = 4 * _exp(-0.5)/_sqrt(2.0)
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
93 _verify('NV_MAGICCONST', 1.71552776992141)
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
94
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
95 TWOPI = 2.0*_pi
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
96 _verify('TWOPI', 6.28318530718)
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
97
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
98 LOG4 = _log(4.0)
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
99 _verify('LOG4', 1.38629436111989)
8393
7669ddf0e7d4 Add Interfaces to replace remaining needs for importing whrandom.
Guido van Rossum <guido@python.org>
parents: 7881
diff changeset
100
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
101 SG_MAGICCONST = 1.0 + _log(4.5)
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
102 _verify('SG_MAGICCONST', 2.50407739677627)
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
103
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
104 del _verify
8393
7669ddf0e7d4 Add Interfaces to replace remaining needs for importing whrandom.
Guido van Rossum <guido@python.org>
parents: 7881
diff changeset
105
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
106 # Translated by Guido van Rossum from C source provided by
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
107 # Adrian Baddeley.
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
108
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
109 class Random:
8393
7669ddf0e7d4 Add Interfaces to replace remaining needs for importing whrandom.
Guido van Rossum <guido@python.org>
parents: 7881
diff changeset
110
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
111 VERSION = 1 # used by getstate/setstate
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
112
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
113 def __init__(self, x=None):
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
114 """Initialize an instance.
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
115
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
116 Optional argument x controls seeding, as for Random.seed().
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
117 """
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
118
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
119 self.seed(x)
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
120 self.gauss_next = None
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
121
16265
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
122 ## -------------------- core generator -------------------
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
123
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
124 # Specific to Wichmann-Hill generator. Subclasses wishing to use a
16243
9fd96e2455b6 Fix bugs introduced by rewrite (in particular, time-based initialization
Tim Peters <tim.peters@gmail.com>
parents: 16242
diff changeset
125 # different core generator should override the seed(), random(),
16265
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
126 # getstate(), setstate() and jumpahead() methods.
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
127
16379
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
128 def seed(self, a=None):
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
129 """Initialize internal state from hashable object.
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
130
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
131 None or no argument seeds from current time.
8393
7669ddf0e7d4 Add Interfaces to replace remaining needs for importing whrandom.
Guido van Rossum <guido@python.org>
parents: 7881
diff changeset
132
16386
c2555b4f38a3 Repaired a docstring.
Tim Peters <tim.peters@gmail.com>
parents: 16379
diff changeset
133 If a is not None or an int or long, hash(a) is used instead.
16379
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
134
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
135 If a is an int or long, a is used directly. Distinct values between
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
136 0 and 27814431486575L inclusive are guaranteed to yield distinct
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
137 internal states (this guarantee is specific to the default
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
138 Wichmann-Hill generator).
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
139 """
8393
7669ddf0e7d4 Add Interfaces to replace remaining needs for importing whrandom.
Guido van Rossum <guido@python.org>
parents: 7881
diff changeset
140
16379
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
141 if a is None:
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
142 # Initialize from current time
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
143 import time
16379
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
144 a = long(time.time() * 256)
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
145
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
146 if type(a) not in (type(3), type(3L)):
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
147 a = hash(a)
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
148
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
149 a, x = divmod(a, 30268)
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
150 a, y = divmod(a, 30306)
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
151 a, z = divmod(a, 30322)
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
152 self._seed = int(x)+1, int(y)+1, int(z)+1
8393
7669ddf0e7d4 Add Interfaces to replace remaining needs for importing whrandom.
Guido van Rossum <guido@python.org>
parents: 7881
diff changeset
153
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
154 def random(self):
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
155 """Get the next random number in the range [0.0, 1.0)."""
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
156
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
157 # Wichman-Hill random number generator.
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
158 #
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
159 # Wichmann, B. A. & Hill, I. D. (1982)
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
160 # Algorithm AS 183:
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
161 # An efficient and portable pseudo-random number generator
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
162 # Applied Statistics 31 (1982) 188-190
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
163 #
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
164 # see also:
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
165 # Correction to Algorithm AS 183
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
166 # Applied Statistics 33 (1984) 123
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
167 #
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
168 # McLeod, A. I. (1985)
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
169 # A remark on Algorithm AS 183
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
170 # Applied Statistics 34 (1985),198-200
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
171
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
172 # This part is thread-unsafe:
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
173 # BEGIN CRITICAL SECTION
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
174 x, y, z = self._seed
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
175 x = (171 * x) % 30269
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
176 y = (172 * y) % 30307
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
177 z = (170 * z) % 30323
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
178 self._seed = x, y, z
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
179 # END CRITICAL SECTION
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
180
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
181 # Note: on a platform using IEEE-754 double arithmetic, this can
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
182 # never return 0.0 (asserted by Tim; proof too long for a comment).
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
183 return (x/30269.0 + y/30307.0 + z/30323.0) % 1.0
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
184
16265
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
185 def getstate(self):
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
186 """Return internal state; can be passed to setstate() later."""
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
187 return self.VERSION, self._seed, self.gauss_next
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
188
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
189 def setstate(self, state):
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
190 """Restore internal state from object returned by getstate()."""
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
191 version = state[0]
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
192 if version == 1:
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
193 version, self._seed, self.gauss_next = state
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
194 else:
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
195 raise ValueError("state with version %s passed to "
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
196 "Random.setstate() of version %s" %
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
197 (version, self.VERSION))
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
198
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
199 def jumpahead(self, n):
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
200 """Act as if n calls to random() were made, but quickly.
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
201
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
202 n is an int, greater than or equal to 0.
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
203
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
204 Example use: If you have 2 threads and know that each will
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
205 consume no more than a million random numbers, create two Random
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
206 objects r1 and r2, then do
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
207 r2.setstate(r1.getstate())
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
208 r2.jumpahead(1000000)
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
209 Then r1 and r2 will use guaranteed-disjoint segments of the full
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
210 period.
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
211 """
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
212
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
213 if not n >= 0:
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
214 raise ValueError("n must be >= 0")
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
215 x, y, z = self._seed
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
216 x = int(x * pow(171, n, 30269)) % 30269
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
217 y = int(y * pow(172, n, 30307)) % 30307
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
218 z = int(z * pow(170, n, 30323)) % 30323
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
219 self._seed = x, y, z
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
220
16379
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
221 def __whseed(self, x=0, y=0, z=0):
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
222 """Set the Wichmann-Hill seed from (x, y, z).
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
223
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
224 These must be integers in the range [0, 256).
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
225 """
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
226
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
227 if not type(x) == type(y) == type(z) == type(0):
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
228 raise TypeError('seeds must be integers')
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
229 if not (0 <= x < 256 and 0 <= y < 256 and 0 <= z < 256):
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
230 raise ValueError('seeds must be in range(0, 256)')
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
231 if 0 == x == y == z:
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
232 # Initialize from current time
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
233 import time
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
234 t = long(time.time() * 256)
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
235 t = int((t&0xffffff) ^ (t>>24))
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
236 t, x = divmod(t, 256)
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
237 t, y = divmod(t, 256)
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
238 t, z = divmod(t, 256)
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
239 # Zero is a poor seed, so substitute 1
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
240 self._seed = (x or 1, y or 1, z or 1)
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
241
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
242 def whseed(self, a=None):
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
243 """Seed from hashable object's hash code.
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
244
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
245 None or no argument seeds from current time. It is not guaranteed
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
246 that objects with distinct hash codes lead to distinct internal
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
247 states.
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
248
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
249 This is obsolete, provided for compatibility with the seed routine
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
250 used prior to Python 2.1. Use the .seed() method instead.
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
251 """
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
252
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
253 if a is None:
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
254 self.__whseed()
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
255 return
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
256 a = hash(a)
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
257 a, x = divmod(a, 256)
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
258 a, y = divmod(a, 256)
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
259 a, z = divmod(a, 256)
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
260 x = (x + a) % 256 or 1
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
261 y = (y + a) % 256 or 1
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
262 z = (z + a) % 256 or 1
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
263 self.__whseed(x, y, z)
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
264
16265
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
265 ## ---- Methods below this point do not need to be overridden when
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
266 ## ---- subclassing for the purpose of using a different core generator.
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
267
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
268 ## -------------------- pickle support -------------------
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
269
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
270 def __getstate__(self): # for pickle
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
271 return self.getstate()
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
272
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
273 def __setstate__(self, state): # for pickle
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
274 self.setstate(state)
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
275
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
276 ## -------------------- integer methods -------------------
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
277
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
278 def randrange(self, start, stop=None, step=1, int=int, default=None):
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
279 """Choose a random item from range(start, stop[, step]).
8393
7669ddf0e7d4 Add Interfaces to replace remaining needs for importing whrandom.
Guido van Rossum <guido@python.org>
parents: 7881
diff changeset
280
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
281 This fixes the problem with randint() which includes the
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
282 endpoint; in Python this is usually not what you want.
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
283 Do not supply the 'int' and 'default' arguments.
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
284 """
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
285
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
286 # This code is a bit messy to make it fast for the
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
287 # common case while still doing adequate error checking
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
288 istart = int(start)
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
289 if istart != start:
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
290 raise ValueError, "non-integer arg 1 for randrange()"
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
291 if stop is default:
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
292 if istart > 0:
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
293 return int(self.random() * istart)
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
294 raise ValueError, "empty range for randrange()"
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
295 istop = int(stop)
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
296 if istop != stop:
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
297 raise ValueError, "non-integer stop for randrange()"
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
298 if step == 1:
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
299 if istart < istop:
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
300 return istart + int(self.random() *
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
301 (istop - istart))
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
302 raise ValueError, "empty range for randrange()"
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
303 istep = int(step)
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
304 if istep != step:
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
305 raise ValueError, "non-integer step for randrange()"
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
306 if istep > 0:
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
307 n = (istop - istart + istep - 1) / istep
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
308 elif istep < 0:
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
309 n = (istop - istart + istep + 1) / istep
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
310 else:
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
311 raise ValueError, "zero step for randrange()"
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
312
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
313 if n <= 0:
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
314 raise ValueError, "empty range for randrange()"
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
315 return istart + istep*int(self.random() * n)
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
316
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
317 def randint(self, a, b):
16265
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
318 """Return random integer in range [a, b], including both end points.
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
319
16265
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
320 (Deprecated; use randrange(a, b+1).)
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
321 """
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
322
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
323 return self.randrange(a, b+1)
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
324
16265
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
325 ## -------------------- sequence methods -------------------
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
326
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
327 def choice(self, seq):
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
328 """Choose a random element from a non-empty sequence."""
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
329 return seq[int(self.random() * len(seq))]
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
330
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
331 def shuffle(self, x, random=None, int=int):
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
332 """x, random=random.random -> shuffle list x in place; return None.
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
333
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
334 Optional arg random is a 0-argument function returning a random
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
335 float in [0.0, 1.0); by default, the standard random.random.
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
336
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
337 Note that for even rather small len(x), the total number of
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
338 permutations of x is larger than the period of most random number
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
339 generators; this implies that "most" permutations of a long
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
340 sequence can never be generated.
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
341 """
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
342
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
343 if random is None:
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
344 random = self.random
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
345 for i in xrange(len(x)-1, 0, -1):
16265
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
346 # pick an element in x[:i+1] with which to exchange x[i]
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
347 j = int(random() * (i+1))
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
348 x[i], x[j] = x[j], x[i]
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
349
16265
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
350 ## -------------------- real-valued distributions -------------------
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
351
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
352 ## -------------------- uniform distribution -------------------
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
353
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
354 def uniform(self, a, b):
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
355 """Get a random number in the range [a, b)."""
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
356 return a + (b-a) * self.random()
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
357
16265
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
358 ## -------------------- normal distribution --------------------
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
359
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
360 def normalvariate(self, mu, sigma):
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
361 # mu = mean, sigma = standard deviation
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
362
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
363 # Uses Kinderman and Monahan method. Reference: Kinderman,
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
364 # A.J. and Monahan, J.F., "Computer generation of random
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
365 # variables using the ratio of uniform deviates", ACM Trans
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
366 # Math Software, 3, (1977), pp257-260.
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
367
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
368 random = self.random
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
369 while 1:
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
370 u1 = random()
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
371 u2 = random()
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
372 z = NV_MAGICCONST*(u1-0.5)/u2
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
373 zz = z*z/4.0
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
374 if zz <= -_log(u2):
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
375 break
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
376 return mu + z*sigma
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
377
16265
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
378 ## -------------------- lognormal distribution --------------------
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
379
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
380 def lognormvariate(self, mu, sigma):
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
381 return _exp(self.normalvariate(mu, sigma))
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
382
16265
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
383 ## -------------------- circular uniform --------------------
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
384
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
385 def cunifvariate(self, mean, arc):
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
386 # mean: mean angle (in radians between 0 and pi)
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
387 # arc: range of distribution (in radians between 0 and pi)
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
388
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
389 return (mean + arc * (self.random() - 0.5)) % _pi
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
390
16265
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
391 ## -------------------- exponential distribution --------------------
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
392
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
393 def expovariate(self, lambd):
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
394 # lambd: rate lambd = 1/mean
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
395 # ('lambda' is a Python reserved word)
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
396
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
397 random = self.random
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14617
diff changeset
398 u = random()
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
399 while u <= 1e-7:
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
400 u = random()
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
401 return -_log(u)/lambd
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
402
16265
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
403 ## -------------------- von Mises distribution --------------------
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
404
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
405 def vonmisesvariate(self, mu, kappa):
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
406 # mu: mean angle (in radians between 0 and 2*pi)
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
407 # kappa: concentration parameter kappa (>= 0)
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
408 # if kappa = 0 generate uniform random angle
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
409
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
410 # Based upon an algorithm published in: Fisher, N.I.,
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
411 # "Statistical Analysis of Circular Data", Cambridge
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
412 # University Press, 1993.
7881
6370f1593c72 Correction to vonmisesvariate() by Magnus Kessler: it should take and
Guido van Rossum <guido@python.org>
parents: 7412
diff changeset
413
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
414 # Thanks to Magnus Kessler for a correction to the
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
415 # implementation of step 4.
7881
6370f1593c72 Correction to vonmisesvariate() by Magnus Kessler: it should take and
Guido van Rossum <guido@python.org>
parents: 7412
diff changeset
416
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
417 random = self.random
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
418 if kappa <= 1e-6:
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
419 return TWOPI * random()
7881
6370f1593c72 Correction to vonmisesvariate() by Magnus Kessler: it should take and
Guido van Rossum <guido@python.org>
parents: 7412
diff changeset
420
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
421 a = 1.0 + _sqrt(1.0 + 4.0 * kappa * kappa)
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
422 b = (a - _sqrt(2.0 * a))/(2.0 * kappa)
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
423 r = (1.0 + b * b)/(2.0 * b)
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
424
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
425 while 1:
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
426 u1 = random()
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
427
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
428 z = _cos(_pi * u1)
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
429 f = (1.0 + r * z)/(r + z)
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
430 c = kappa * (r - f)
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
431
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
432 u2 = random()
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
433
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
434 if not (u2 >= c * (2.0 - c) and u2 > c * _exp(1.0 - c)):
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
435 break
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
436
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
437 u3 = random()
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
438 if u3 > 0.5:
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
439 theta = (mu % TWOPI) + _acos(f)
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
440 else:
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
441 theta = (mu % TWOPI) - _acos(f)
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
442
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
443 return theta
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
444
16265
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
445 ## -------------------- gamma distribution --------------------
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
446
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
447 def gammavariate(self, alpha, beta):
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
448 # beta times standard gamma
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
449 ainv = _sqrt(2.0 * alpha - 1.0)
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
450 return beta * self.stdgamma(alpha, ainv, alpha - LOG4, alpha + ainv)
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
451
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
452 def stdgamma(self, alpha, ainv, bbb, ccc):
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
453 # ainv = sqrt(2 * alpha - 1)
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
454 # bbb = alpha - log(4)
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
455 # ccc = alpha + ainv
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
456
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
457 random = self.random
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
458 if alpha <= 0.0:
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
459 raise ValueError, 'stdgamma: alpha must be > 0.0'
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
460
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
461 if alpha > 1.0:
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
462
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
463 # Uses R.C.H. Cheng, "The generation of Gamma
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
464 # variables with non-integral shape parameters",
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
465 # Applied Statistics, (1977), 26, No. 1, p71-74
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
466
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
467 while 1:
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
468 u1 = random()
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
469 u2 = random()
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
470 v = _log(u1/(1.0-u1))/ainv
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
471 x = alpha*_exp(v)
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
472 z = u1*u1*u2
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
473 r = bbb+ccc*v-x
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
474 if r + SG_MAGICCONST - 4.5*z >= 0.0 or r >= _log(z):
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
475 return x
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
476
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
477 elif alpha == 1.0:
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
478 # expovariate(1)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14617
diff changeset
479 u = random()
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
480 while u <= 1e-7:
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
481 u = random()
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
482 return -_log(u)
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
483
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
484 else: # alpha is between 0 and 1 (exclusive)
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
485
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
486 # Uses ALGORITHM GS of Statistical Computing - Kennedy & Gentle
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
487
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
488 while 1:
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
489 u = random()
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
490 b = (_e + alpha)/_e
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
491 p = b*u
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
492 if p <= 1.0:
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
493 x = pow(p, 1.0/alpha)
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
494 else:
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
495 # p > 1
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
496 x = -_log((b-p)/alpha)
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
497 u1 = random()
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
498 if not (((p <= 1.0) and (u1 > _exp(-x))) or
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
499 ((p > 1) and (u1 > pow(x, alpha - 1.0)))):
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
500 break
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
501 return x
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
502
1576
695fb494a3a2 Added gauss() (same as normal but twice as fast) and betavariate();
Guido van Rossum <guido@python.org>
parents: 1575
diff changeset
503
16265
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
504 ## -------------------- Gauss (faster alternative) --------------------
1576
695fb494a3a2 Added gauss() (same as normal but twice as fast) and betavariate();
Guido van Rossum <guido@python.org>
parents: 1575
diff changeset
505
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
506 def gauss(self, mu, sigma):
1577
444e349f962f Use float constants directly; cosmetic changes; initialize largest
Guido van Rossum <guido@python.org>
parents: 1576
diff changeset
507
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
508 # When x and y are two variables from [0, 1), uniformly
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
509 # distributed, then
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
510 #
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
511 # cos(2*pi*x)*sqrt(-2*log(1-y))
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
512 # sin(2*pi*x)*sqrt(-2*log(1-y))
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
513 #
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
514 # are two *independent* variables with normal distribution
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
515 # (mu = 0, sigma = 1).
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
516 # (Lambert Meertens)
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
517 # (corrected version; bug discovered by Mike Miller, fixed by LM)
1577
444e349f962f Use float constants directly; cosmetic changes; initialize largest
Guido van Rossum <guido@python.org>
parents: 1576
diff changeset
518
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
519 # Multithreading note: When two threads call this function
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
520 # simultaneously, it is possible that they will receive the
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
521 # same return value. The window is very small though. To
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
522 # avoid this, you have to use a lock around all calls. (I
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
523 # didn't want to slow this down in the serial case by using a
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
524 # lock here.)
1577
444e349f962f Use float constants directly; cosmetic changes; initialize largest
Guido van Rossum <guido@python.org>
parents: 1576
diff changeset
525
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
526 random = self.random
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
527 z = self.gauss_next
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
528 self.gauss_next = None
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
529 if z is None:
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
530 x2pi = random() * TWOPI
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
531 g2rad = _sqrt(-2.0 * _log(1.0 - random()))
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
532 z = _cos(x2pi) * g2rad
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
533 self.gauss_next = _sin(x2pi) * g2rad
1577
444e349f962f Use float constants directly; cosmetic changes; initialize largest
Guido van Rossum <guido@python.org>
parents: 1576
diff changeset
534
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
535 return mu + z*sigma
1576
695fb494a3a2 Added gauss() (same as normal but twice as fast) and betavariate();
Guido van Rossum <guido@python.org>
parents: 1575
diff changeset
536
16265
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
537 ## -------------------- beta --------------------
16271
7ef2382f6123 SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents: 16265
diff changeset
538 ## See
7ef2382f6123 SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents: 16265
diff changeset
539 ## http://sourceforge.net/bugs/?func=detailbug&bug_id=130030&group_id=5470
7ef2382f6123 SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents: 16265
diff changeset
540 ## for Ivan Frohne's insightful analysis of why the original implementation:
7ef2382f6123 SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents: 16265
diff changeset
541 ##
7ef2382f6123 SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents: 16265
diff changeset
542 ## def betavariate(self, alpha, beta):
7ef2382f6123 SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents: 16265
diff changeset
543 ## # Discrete Event Simulation in C, pp 87-88.
7ef2382f6123 SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents: 16265
diff changeset
544 ##
7ef2382f6123 SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents: 16265
diff changeset
545 ## y = self.expovariate(alpha)
7ef2382f6123 SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents: 16265
diff changeset
546 ## z = self.expovariate(1.0/beta)
7ef2382f6123 SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents: 16265
diff changeset
547 ## return z/(y+z)
7ef2382f6123 SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents: 16265
diff changeset
548 ##
7ef2382f6123 SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents: 16265
diff changeset
549 ## was dead wrong, and how it probably got that way.
1576
695fb494a3a2 Added gauss() (same as normal but twice as fast) and betavariate();
Guido van Rossum <guido@python.org>
parents: 1575
diff changeset
550
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
551 def betavariate(self, alpha, beta):
16271
7ef2382f6123 SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents: 16265
diff changeset
552 # This version due to Janne Sinkkonen, and matches all the std
7ef2382f6123 SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents: 16265
diff changeset
553 # texts (e.g., Knuth Vol 2 Ed 3 pg 134 "the beta distribution").
7ef2382f6123 SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents: 16265
diff changeset
554 y = self.gammavariate(alpha, 1.)
7ef2382f6123 SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents: 16265
diff changeset
555 if y == 0:
7ef2382f6123 SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents: 16265
diff changeset
556 return 0.0
7ef2382f6123 SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents: 16265
diff changeset
557 else:
7ef2382f6123 SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents: 16265
diff changeset
558 return y / (y + self.gammavariate(beta, 1.))
1576
695fb494a3a2 Added gauss() (same as normal but twice as fast) and betavariate();
Guido van Rossum <guido@python.org>
parents: 1575
diff changeset
559
16265
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
560 ## -------------------- Pareto --------------------
6698
b73eb4e6face Added Pareto and Weibull distributions, courtesy Jeremy Hylton.
Guido van Rossum <guido@python.org>
parents: 1618
diff changeset
561
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
562 def paretovariate(self, alpha):
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
563 # Jain, pg. 495
6698
b73eb4e6face Added Pareto and Weibull distributions, courtesy Jeremy Hylton.
Guido van Rossum <guido@python.org>
parents: 1618
diff changeset
564
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
565 u = self.random()
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
566 return 1.0 / pow(u, 1.0/alpha)
6698
b73eb4e6face Added Pareto and Weibull distributions, courtesy Jeremy Hylton.
Guido van Rossum <guido@python.org>
parents: 1618
diff changeset
567
16265
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
568 ## -------------------- Weibull --------------------
6698
b73eb4e6face Added Pareto and Weibull distributions, courtesy Jeremy Hylton.
Guido van Rossum <guido@python.org>
parents: 1618
diff changeset
569
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
570 def weibullvariate(self, alpha, beta):
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
571 # Jain, pg. 499; bug fix courtesy Bill Arms
11200
bca2d14f3e36 Add Tim Peters' shuffle() algorithm.
Guido van Rossum <guido@python.org>
parents: 8802
diff changeset
572
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
573 u = self.random()
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
574 return alpha * pow(-_log(u), 1.0/beta)
11200
bca2d14f3e36 Add Tim Peters' shuffle() algorithm.
Guido van Rossum <guido@python.org>
parents: 8802
diff changeset
575
16265
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
576 ## -------------------- test program --------------------
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
577
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
578 def _test_generator(n, funccall):
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14617
diff changeset
579 import time
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14617
diff changeset
580 print n, 'times', funccall
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14617
diff changeset
581 code = compile(funccall, funccall, 'eval')
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14617
diff changeset
582 sum = 0.0
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14617
diff changeset
583 sqsum = 0.0
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14617
diff changeset
584 smallest = 1e10
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14617
diff changeset
585 largest = -1e10
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14617
diff changeset
586 t0 = time.time()
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14617
diff changeset
587 for i in range(n):
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14617
diff changeset
588 x = eval(code)
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14617
diff changeset
589 sum = sum + x
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14617
diff changeset
590 sqsum = sqsum + x*x
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14617
diff changeset
591 smallest = min(x, smallest)
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14617
diff changeset
592 largest = max(x, largest)
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14617
diff changeset
593 t1 = time.time()
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14617
diff changeset
594 print round(t1-t0, 3), 'sec,',
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14617
diff changeset
595 avg = sum/n
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
596 stddev = _sqrt(sqsum/n - avg*avg)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14617
diff changeset
597 print 'avg %g, stddev %g, min %g, max %g' % \
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14617
diff changeset
598 (avg, stddev, smallest, largest)
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
599
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
600 def _test(N=200):
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
601 print 'TWOPI =', TWOPI
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
602 print 'LOG4 =', LOG4
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
603 print 'NV_MAGICCONST =', NV_MAGICCONST
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
604 print 'SG_MAGICCONST =', SG_MAGICCONST
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
605 _test_generator(N, 'random()')
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
606 _test_generator(N, 'normalvariate(0.0, 1.0)')
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
607 _test_generator(N, 'lognormvariate(0.0, 1.0)')
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
608 _test_generator(N, 'cunifvariate(0.0, 1.0)')
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
609 _test_generator(N, 'expovariate(1.0)')
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
610 _test_generator(N, 'vonmisesvariate(0.0, 1.0)')
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
611 _test_generator(N, 'gammavariate(0.5, 1.0)')
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
612 _test_generator(N, 'gammavariate(0.9, 1.0)')
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
613 _test_generator(N, 'gammavariate(1.0, 1.0)')
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
614 _test_generator(N, 'gammavariate(2.0, 1.0)')
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
615 _test_generator(N, 'gammavariate(20.0, 1.0)')
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
616 _test_generator(N, 'gammavariate(200.0, 1.0)')
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
617 _test_generator(N, 'gauss(0.0, 1.0)')
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
618 _test_generator(N, 'betavariate(3.0, 3.0)')
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
619 _test_generator(N, 'paretovariate(1.0)')
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
620 _test_generator(N, 'weibullvariate(1.0, 1.0)')
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
621
16265
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
622 # Test jumpahead.
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
623 s = getstate()
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
624 jumpahead(N)
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
625 r1 = random()
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
626 # now do it the slow way
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
627 setstate(s)
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
628 for i in range(N):
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
629 random()
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
630 r2 = random()
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
631 if r1 != r2:
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
632 raise ValueError("jumpahead test failed " + `(N, r1, r2)`)
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
633
16292
0501c2a10315 New comment block to Clarify a subtlety.
Tim Peters <tim.peters@gmail.com>
parents: 16272
diff changeset
634 # Create one instance, seeded from current time, and export its methods
0501c2a10315 New comment block to Clarify a subtlety.
Tim Peters <tim.peters@gmail.com>
parents: 16272
diff changeset
635 # as module-level functions. The functions are not threadsafe, and state
0501c2a10315 New comment block to Clarify a subtlety.
Tim Peters <tim.peters@gmail.com>
parents: 16272
diff changeset
636 # is shared across all uses (both in the user's code and in the Python
0501c2a10315 New comment block to Clarify a subtlety.
Tim Peters <tim.peters@gmail.com>
parents: 16272
diff changeset
637 # libraries), but that's fine for most programs and is easier for the
0501c2a10315 New comment block to Clarify a subtlety.
Tim Peters <tim.peters@gmail.com>
parents: 16272
diff changeset
638 # casual user than making them instantiate their own Random() instance.
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
639 _inst = Random()
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
640 seed = _inst.seed
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
641 random = _inst.random
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
642 uniform = _inst.uniform
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
643 randint = _inst.randint
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
644 choice = _inst.choice
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
645 randrange = _inst.randrange
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
646 shuffle = _inst.shuffle
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
647 normalvariate = _inst.normalvariate
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
648 lognormvariate = _inst.lognormvariate
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
649 cunifvariate = _inst.cunifvariate
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
650 expovariate = _inst.expovariate
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
651 vonmisesvariate = _inst.vonmisesvariate
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
652 gammavariate = _inst.gammavariate
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
653 stdgamma = _inst.stdgamma
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
654 gauss = _inst.gauss
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
655 betavariate = _inst.betavariate
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
656 paretovariate = _inst.paretovariate
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
657 weibullvariate = _inst.weibullvariate
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
658 getstate = _inst.getstate
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
659 setstate = _inst.setstate
16243
9fd96e2455b6 Fix bugs introduced by rewrite (in particular, time-based initialization
Tim Peters <tim.peters@gmail.com>
parents: 16242
diff changeset
660 jumpahead = _inst.jumpahead
16379
5f0683a69efc Change random.seed() so that it can get at the full range of possible
Tim Peters <tim.peters@gmail.com>
parents: 16292
diff changeset
661 whseed = _inst.whseed
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
662
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
663 if __name__ == '__main__':
16242
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
664 _test()