annotate Lib/random.py @ 25135:0f0016632be4 legacy-trunk

_formatparam(), set_param(): RFC 2231 encoding support by Oleg Broytmann in SF patch #600096. Specifically, the former function now encodes the triplets, while the latter adds optional charset and language arguments.
author Barry Warsaw <barry@python.org>
date Fri, 06 Sep 2002 03:55:04 +0000
parents 4e795fe9f54b
children 8ceb455f5541
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
24861
4e795fe9f54b SF bug 594996: OverflowError in random.randrange
Tim Peters <tim.peters@gmail.com>
parents: 23536
diff changeset
78 from math import floor as _floor
8455
af9273b1073a Make gauss() semi-thread-safe. It can still give duplicate results,
Guido van Rossum <guido@python.org>
parents: 8393
diff changeset
79
16613
f68dbffff927 bunch more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 16386
diff changeset
80 __all__ = ["Random","seed","random","uniform","randint","choice",
f68dbffff927 bunch more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 16386
diff changeset
81 "randrange","shuffle","normalvariate","lognormvariate",
f68dbffff927 bunch more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 16386
diff changeset
82 "cunifvariate","expovariate","vonmisesvariate","gammavariate",
f68dbffff927 bunch more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 16386
diff changeset
83 "stdgamma","gauss","betavariate","paretovariate","weibullvariate",
f68dbffff927 bunch more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 16386
diff changeset
84 "getstate","setstate","jumpahead","whseed"]
16616
e2a63584566f Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 16613
diff changeset
85
21049
fed5071da957 SF patch 483059: Avoid use of eval() in random.py, from Finn Bock.
Tim Peters <tim.peters@gmail.com>
parents: 16616
diff changeset
86 def _verify(name, computed, expected):
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
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)
21049
fed5071da957 SF patch 483059: Avoid use of eval() in random.py, from Finn Bock.
Tim Peters <tim.peters@gmail.com>
parents: 16616
diff changeset
93 _verify('NV_MAGICCONST', NV_MAGICCONST, 1.71552776992141)
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
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
21049
fed5071da957 SF patch 483059: Avoid use of eval() in random.py, from Finn Bock.
Tim Peters <tim.peters@gmail.com>
parents: 16616
diff changeset
96 _verify('TWOPI', TWOPI, 6.28318530718)
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
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)
21049
fed5071da957 SF patch 483059: Avoid use of eval() in random.py, from Finn Bock.
Tim Peters <tim.peters@gmail.com>
parents: 16616
diff changeset
99 _verify('LOG4', 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)
21049
fed5071da957 SF patch 483059: Avoid use of eval() in random.py, from Finn Bock.
Tim Peters <tim.peters@gmail.com>
parents: 16616
diff changeset
102 _verify('SG_MAGICCONST', SG_MAGICCONST, 2.50407739677627)
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
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:
23531
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
110 """Random number generator base class used by bound module functions.
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
111
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
112 Used to instantiate instances of Random to get generators that don't
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
113 share state. Especially useful for multi-threaded programs, creating
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
114 a different instance of Random for each thread, and using the jumpahead()
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
115 method to ensure that the generated sequences seen by each thread don't
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
116 overlap.
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
117
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
118 Class Random can also be subclassed if you want to use a different basic
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
119 generator of your own devising: in that case, override the following
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
120 methods: random(), seed(), getstate(), setstate() and jumpahead().
23536
0a4c92559a0b Whitespace normalized.
Raymond Hettinger <python@rcn.com>
parents: 23531
diff changeset
121
23531
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
122 """
8393
7669ddf0e7d4 Add Interfaces to replace remaining needs for importing whrandom.
Guido van Rossum <guido@python.org>
parents: 7881
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 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
125
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
126 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
127 """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
128
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
129 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
130 """
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
131
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
132 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
133
16265
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
134 ## -------------------- core generator -------------------
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
135
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
136 # 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
137 # 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
138 # 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
139
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
140 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
141 """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
142
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
143 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
144
16386
c2555b4f38a3 Repaired a docstring.
Tim Peters <tim.peters@gmail.com>
parents: 16379
diff changeset
145 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
146
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 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
148 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
149 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
150 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
151 """
8393
7669ddf0e7d4 Add Interfaces to replace remaining needs for importing whrandom.
Guido van Rossum <guido@python.org>
parents: 7881
diff changeset
152
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
153 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
154 # 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
155 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
156 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
157
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
158 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
159 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
160
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
161 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
162 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
163 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
164 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
165
23380
9be01b7b6c80 random.gauss() uses a piece of hidden state used by nothing else,
Tim Peters <tim.peters@gmail.com>
parents: 23023
diff changeset
166 self.gauss_next = None
9be01b7b6c80 random.gauss() uses a piece of hidden state used by nothing else,
Tim Peters <tim.peters@gmail.com>
parents: 23023
diff changeset
167
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
168 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
169 """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
170
3c5f09422b69 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 # 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
172 #
3c5f09422b69 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 # 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
174 # 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
175 # 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
176 # 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
177 #
3c5f09422b69 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 # 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
179 # 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
180 # 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
181 #
3c5f09422b69 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 # 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
183 # 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
184 # 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
185
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
186 # 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
187 # 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
188 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
189 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
190 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
191 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
192 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
193 # 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
194
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
195 # 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
196 # 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
197 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
198
16265
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
199 def getstate(self):
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
200 """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
201 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
202
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
203 def setstate(self, state):
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
204 """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
205 version = state[0]
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
206 if version == 1:
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
207 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
208 else:
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
209 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
210 "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
211 (version, self.VERSION))
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 def jumpahead(self, n):
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
214 """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
215
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
216 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
217
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
218 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
219 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
220 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
221 r2.setstate(r1.getstate())
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
222 r2.jumpahead(1000000)
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
223 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
224 period.
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
225 """
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
226
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
227 if not n >= 0:
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
228 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
229 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
230 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
231 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
232 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
233 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
234
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
235 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
236 """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
237
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 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
239 """
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
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 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
242 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
243 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
244 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
245 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
246 # 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
247 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
248 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
249 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
250 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
251 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
252 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
253 # 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
254 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
255
23380
9be01b7b6c80 random.gauss() uses a piece of hidden state used by nothing else,
Tim Peters <tim.peters@gmail.com>
parents: 23023
diff changeset
256 self.gauss_next = None
9be01b7b6c80 random.gauss() uses a piece of hidden state used by nothing else,
Tim Peters <tim.peters@gmail.com>
parents: 23023
diff changeset
257
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
258 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
259 """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
260
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 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
262 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
263 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
264
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
265 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
266 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
267 """
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
268
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
269 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
270 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
271 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
272 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
273 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
274 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
275 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
276 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
277 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
278 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
279 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
280
16265
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
281 ## ---- 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
282 ## ---- 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
283
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
284 ## -------------------- pickle support -------------------
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
285
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
286 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
287 return self.getstate()
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
288
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
289 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
290 self.setstate(state)
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
291
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
292 ## -------------------- integer methods -------------------
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
293
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
294 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
295 """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
296
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
297 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
298 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
299 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
300 """
3c5f09422b69 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
3c5f09422b69 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 # This code is a bit messy to make it fast for the
24861
4e795fe9f54b SF bug 594996: OverflowError in random.randrange
Tim Peters <tim.peters@gmail.com>
parents: 23536
diff changeset
303 # common case while still doing adequate error checking.
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
304 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
305 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
306 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
307 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
308 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
309 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
310 raise ValueError, "empty range for randrange()"
24861
4e795fe9f54b SF bug 594996: OverflowError in random.randrange
Tim Peters <tim.peters@gmail.com>
parents: 23536
diff changeset
311
4e795fe9f54b SF bug 594996: OverflowError in random.randrange
Tim Peters <tim.peters@gmail.com>
parents: 23536
diff changeset
312 # stop argument supplied.
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
313 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
314 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
315 raise ValueError, "non-integer stop for randrange()"
24861
4e795fe9f54b SF bug 594996: OverflowError in random.randrange
Tim Peters <tim.peters@gmail.com>
parents: 23536
diff changeset
316 if step == 1 and istart < istop:
4e795fe9f54b SF bug 594996: OverflowError in random.randrange
Tim Peters <tim.peters@gmail.com>
parents: 23536
diff changeset
317 try:
4e795fe9f54b SF bug 594996: OverflowError in random.randrange
Tim Peters <tim.peters@gmail.com>
parents: 23536
diff changeset
318 return istart + int(self.random()*(istop - istart))
4e795fe9f54b SF bug 594996: OverflowError in random.randrange
Tim Peters <tim.peters@gmail.com>
parents: 23536
diff changeset
319 except OverflowError:
4e795fe9f54b SF bug 594996: OverflowError in random.randrange
Tim Peters <tim.peters@gmail.com>
parents: 23536
diff changeset
320 # This can happen if istop-istart > sys.maxint + 1, and
4e795fe9f54b SF bug 594996: OverflowError in random.randrange
Tim Peters <tim.peters@gmail.com>
parents: 23536
diff changeset
321 # multiplying by random() doesn't reduce it to something
4e795fe9f54b SF bug 594996: OverflowError in random.randrange
Tim Peters <tim.peters@gmail.com>
parents: 23536
diff changeset
322 # <= sys.maxint. We know that the overall result fits
4e795fe9f54b SF bug 594996: OverflowError in random.randrange
Tim Peters <tim.peters@gmail.com>
parents: 23536
diff changeset
323 # in an int, and can still do it correctly via math.floor().
4e795fe9f54b SF bug 594996: OverflowError in random.randrange
Tim Peters <tim.peters@gmail.com>
parents: 23536
diff changeset
324 # But that adds another function call, so for speed we
4e795fe9f54b SF bug 594996: OverflowError in random.randrange
Tim Peters <tim.peters@gmail.com>
parents: 23536
diff changeset
325 # avoided that whenever possible.
4e795fe9f54b SF bug 594996: OverflowError in random.randrange
Tim Peters <tim.peters@gmail.com>
parents: 23536
diff changeset
326 return int(istart + _floor(self.random()*(istop - istart)))
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 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
328 raise ValueError, "empty range for randrange()"
24861
4e795fe9f54b SF bug 594996: OverflowError in random.randrange
Tim Peters <tim.peters@gmail.com>
parents: 23536
diff changeset
329
4e795fe9f54b SF bug 594996: OverflowError in random.randrange
Tim Peters <tim.peters@gmail.com>
parents: 23536
diff changeset
330 # Non-unit step argument supplied.
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
331 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
332 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
333 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
334 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
335 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
336 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
337 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
338 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
339 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
340
3c5f09422b69 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 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
342 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
343 return istart + istep*int(self.random() * n)
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
344
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
345 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
346 """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
347 """
3c5f09422b69 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
3c5f09422b69 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 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
350
16265
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
351 ## -------------------- sequence methods -------------------
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
352
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 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
354 """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
355 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
356
3c5f09422b69 Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents: 15871
diff changeset
357 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
358 """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
359
3c5f09422b69 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 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
361 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
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 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
364 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
365 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
366 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
367 """
3c5f09422b69 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
3c5f09422b69 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 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
370 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
371 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
372 # 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
373 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
374 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
375
16265
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
376 ## -------------------- real-valued distributions -------------------
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
377
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
378 ## -------------------- 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
379
3c5f09422b69 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 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
381 """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
382 return a + (b-a) * self.random()
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
383
16265
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
384 ## -------------------- normal distribution --------------------
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
385
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
386 def normalvariate(self, mu, sigma):
23531
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
387 """Normal distribution.
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
388
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
389 mu is the mean, and sigma is the standard deviation.
23536
0a4c92559a0b Whitespace normalized.
Raymond Hettinger <python@rcn.com>
parents: 23531
diff changeset
390
23531
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
391 """
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
392 # 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
393
3c5f09422b69 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 # 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
395 # 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
396 # 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
397 # Math Software, 3, (1977), pp257-260.
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
398
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 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
400 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
401 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
402 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
403 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
404 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
405 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
406 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
407 return mu + z*sigma
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
408
16265
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
409 ## -------------------- lognormal distribution --------------------
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
410
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
411 def lognormvariate(self, mu, sigma):
23531
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
412 """Log normal distribution.
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
413
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
414 If you take the natural logarithm of this distribution, you'll get a
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
415 normal distribution with mean mu and standard deviation sigma.
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
416 mu can have any value, and sigma must be greater than zero.
23536
0a4c92559a0b Whitespace normalized.
Raymond Hettinger <python@rcn.com>
parents: 23531
diff changeset
417
23531
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
418 """
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
419 return _exp(self.normalvariate(mu, sigma))
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
420
16265
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
421 ## -------------------- circular uniform --------------------
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
422
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
423 def cunifvariate(self, mean, arc):
23531
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
424 """Circular uniform distribution.
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
425
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
426 mean is the mean angle, and arc is the range of the distribution,
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
427 centered around the mean angle. Both values must be expressed in
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
428 radians. Returned values range between mean - arc/2 and
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
429 mean + arc/2 and are normalized to between 0 and pi.
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
430
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
431 Deprecated in version 2.3. Use:
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
432 (mean + arc * (Random.random() - 0.5)) % Math.pi
23536
0a4c92559a0b Whitespace normalized.
Raymond Hettinger <python@rcn.com>
parents: 23531
diff changeset
433
23531
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
434 """
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
435 # 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
436 # arc: range of distribution (in radians between 0 and pi)
23531
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
437 import warnings
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
438 warnings.warn("The cunifvariate function is deprecated; Use (mean "
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
439 "+ arc * (Random.random() - 0.5)) % Math.pi instead",
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
440 DeprecationWarning)
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
441
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
442 return (mean + arc * (self.random() - 0.5)) % _pi
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
443
16265
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
444 ## -------------------- exponential distribution --------------------
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
445
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
446 def expovariate(self, lambd):
23531
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
447 """Exponential distribution.
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
448
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
449 lambd is 1.0 divided by the desired mean. (The parameter would be
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
450 called "lambda", but that is a reserved word in Python.) Returned
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
451 values range from 0 to positive infinity.
23536
0a4c92559a0b Whitespace normalized.
Raymond Hettinger <python@rcn.com>
parents: 23531
diff changeset
452
23531
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
453 """
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
454 # 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
455 # ('lambda' is a Python reserved word)
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
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14617
diff changeset
458 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
459 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
460 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
461 return -_log(u)/lambd
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
462
16265
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
463 ## -------------------- von Mises distribution --------------------
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
464
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
465 def vonmisesvariate(self, mu, kappa):
23531
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
466 """Circular data distribution.
23536
0a4c92559a0b Whitespace normalized.
Raymond Hettinger <python@rcn.com>
parents: 23531
diff changeset
467
23531
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
468 mu is the mean angle, expressed in radians between 0 and 2*pi, and
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
469 kappa is the concentration parameter, which must be greater than or
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
470 equal to zero. If kappa is equal to zero, this distribution reduces
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
471 to a uniform random angle over the range 0 to 2*pi.
23536
0a4c92559a0b Whitespace normalized.
Raymond Hettinger <python@rcn.com>
parents: 23531
diff changeset
472
23531
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
473 """
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
474 # 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
475 # 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
476 # if kappa = 0 generate uniform random angle
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
477
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
478 # 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
479 # "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
480 # 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
481
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
482 # 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
483 # 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
484
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
485 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
486 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
487 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
488
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
489 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
490 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
491 r = (1.0 + b * b)/(2.0 * b)
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
492
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
493 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
494 u1 = random()
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
495
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
496 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
497 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
498 c = kappa * (r - f)
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
499
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
500 u2 = random()
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
501
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
502 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
503 break
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
504
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
505 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
506 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
507 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
508 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
509 theta = (mu % TWOPI) - _acos(f)
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
510
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
511 return theta
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
512
16265
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
513 ## -------------------- gamma distribution --------------------
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
514
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
515 def gammavariate(self, alpha, beta):
23531
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
516 """Gamma distribution. Not the gamma function!
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
517
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
518 Conditions on the parameters are alpha > 0 and beta > 0.
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
519
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
520 """
23521
a90faa5f9417 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23451
diff changeset
521
23447
08ccb0f3e2d4 Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents: 23442
diff changeset
522 # alpha > 0, beta > 0, mean is alpha*beta, variance is alpha*beta**2
23521
a90faa5f9417 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23451
diff changeset
523
23451
273dd2508c57 Use spaces, not tabs for indents.
Guido van Rossum <guido@python.org>
parents: 23447
diff changeset
524 # Warning: a few older sources define the gamma distribution in terms
273dd2508c57 Use spaces, not tabs for indents.
Guido van Rossum <guido@python.org>
parents: 23447
diff changeset
525 # of alpha > -1.0
273dd2508c57 Use spaces, not tabs for indents.
Guido van Rossum <guido@python.org>
parents: 23447
diff changeset
526 if alpha <= 0.0 or beta <= 0.0:
273dd2508c57 Use spaces, not tabs for indents.
Guido van Rossum <guido@python.org>
parents: 23447
diff changeset
527 raise ValueError, 'gammavariate: alpha and beta must be > 0.0'
23521
a90faa5f9417 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23451
diff changeset
528
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
529 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
530 if alpha > 1.0:
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
531
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
532 # 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
533 # 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
534 # Applied Statistics, (1977), 26, No. 1, p71-74
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
535
23442
b15896053933 Closes SF bug 527139. Moved temp variables inside guard.
Raymond Hettinger <python@rcn.com>
parents: 23380
diff changeset
536 ainv = _sqrt(2.0 * alpha - 1.0)
b15896053933 Closes SF bug 527139. Moved temp variables inside guard.
Raymond Hettinger <python@rcn.com>
parents: 23380
diff changeset
537 bbb = alpha - LOG4
b15896053933 Closes SF bug 527139. Moved temp variables inside guard.
Raymond Hettinger <python@rcn.com>
parents: 23380
diff changeset
538 ccc = alpha + ainv
23521
a90faa5f9417 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23451
diff changeset
539
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
540 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
541 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
542 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
543 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
544 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
545 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
546 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
547 if r + SG_MAGICCONST - 4.5*z >= 0.0 or r >= _log(z):
23447
08ccb0f3e2d4 Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents: 23442
diff changeset
548 return x * beta
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
549
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
550 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
551 # expovariate(1)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14617
diff changeset
552 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
553 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
554 u = random()
23447
08ccb0f3e2d4 Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents: 23442
diff changeset
555 return -_log(u) * beta
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
556
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
557 else: # alpha is between 0 and 1 (exclusive)
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
558
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
559 # Uses ALGORITHM GS of Statistical Computing - Kennedy & Gentle
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
560
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
561 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
562 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
563 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
564 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
565 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
566 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
567 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
568 # 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
569 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
570 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
571 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
572 ((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
573 break
23447
08ccb0f3e2d4 Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents: 23442
diff changeset
574 return x * beta
08ccb0f3e2d4 Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents: 23442
diff changeset
575
08ccb0f3e2d4 Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents: 23442
diff changeset
576
08ccb0f3e2d4 Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents: 23442
diff changeset
577 def stdgamma(self, alpha, ainv, bbb, ccc):
08ccb0f3e2d4 Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents: 23442
diff changeset
578 # This method was (and shall remain) undocumented.
08ccb0f3e2d4 Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents: 23442
diff changeset
579 # This method is deprecated
08ccb0f3e2d4 Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents: 23442
diff changeset
580 # for the following reasons:
08ccb0f3e2d4 Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents: 23442
diff changeset
581 # 1. Returns same as .gammavariate(alpha, 1.0)
08ccb0f3e2d4 Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents: 23442
diff changeset
582 # 2. Requires caller to provide 3 extra arguments
08ccb0f3e2d4 Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents: 23442
diff changeset
583 # that are functions of alpha anyway
08ccb0f3e2d4 Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents: 23442
diff changeset
584 # 3. Can't be used for alpha < 0.5
08ccb0f3e2d4 Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents: 23442
diff changeset
585
08ccb0f3e2d4 Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents: 23442
diff changeset
586 # ainv = sqrt(2 * alpha - 1)
08ccb0f3e2d4 Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents: 23442
diff changeset
587 # bbb = alpha - log(4)
08ccb0f3e2d4 Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents: 23442
diff changeset
588 # ccc = alpha + ainv
08ccb0f3e2d4 Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents: 23442
diff changeset
589 import warnings
08ccb0f3e2d4 Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents: 23442
diff changeset
590 warnings.warn("The stdgamma function is deprecated; "
08ccb0f3e2d4 Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents: 23442
diff changeset
591 "use gammavariate() instead",
08ccb0f3e2d4 Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents: 23442
diff changeset
592 DeprecationWarning)
08ccb0f3e2d4 Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents: 23442
diff changeset
593 return self.gammavariate(alpha, 1.0)
08ccb0f3e2d4 Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents: 23442
diff changeset
594
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
595
1576
695fb494a3a2 Added gauss() (same as normal but twice as fast) and betavariate();
Guido van Rossum <guido@python.org>
parents: 1575
diff changeset
596
16265
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
597 ## -------------------- 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
598
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
599 def gauss(self, mu, sigma):
23531
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
600 """Gaussian distribution.
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
601
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
602 mu is the mean, and sigma is the standard deviation. This is
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
603 slightly faster than the normalvariate() function.
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
604
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
605 Not thread-safe without a lock around calls.
23536
0a4c92559a0b Whitespace normalized.
Raymond Hettinger <python@rcn.com>
parents: 23531
diff changeset
606
23531
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
607 """
1577
444e349f962f Use float constants directly; cosmetic changes; initialize largest
Guido van Rossum <guido@python.org>
parents: 1576
diff changeset
608
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
609 # 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
610 # 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
611 #
3c5f09422b69 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 # 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
613 # 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
614 #
3c5f09422b69 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 # 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
616 # (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
617 # (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
618 # (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
619
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
620 # 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
621 # 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
622 # 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
623 # 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
624 # 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
625 # lock here.)
1577
444e349f962f Use float constants directly; cosmetic changes; initialize largest
Guido van Rossum <guido@python.org>
parents: 1576
diff changeset
626
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
627 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
628 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
629 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
630 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
631 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
632 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
633 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
634 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
635
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
636 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
637
16265
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
638 ## -------------------- beta --------------------
16271
7ef2382f6123 SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents: 16265
diff changeset
639 ## See
7ef2382f6123 SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents: 16265
diff changeset
640 ## 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
641 ## 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
642 ##
7ef2382f6123 SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents: 16265
diff changeset
643 ## def betavariate(self, alpha, beta):
7ef2382f6123 SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents: 16265
diff changeset
644 ## # 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
645 ##
7ef2382f6123 SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents: 16265
diff changeset
646 ## y = self.expovariate(alpha)
7ef2382f6123 SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents: 16265
diff changeset
647 ## 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
648 ## return z/(y+z)
7ef2382f6123 SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents: 16265
diff changeset
649 ##
7ef2382f6123 SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents: 16265
diff changeset
650 ## 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
651
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
652 def betavariate(self, alpha, beta):
23531
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
653 """Beta distribution.
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
654
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
655 Conditions on the parameters are alpha > -1 and beta} > -1.
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
656 Returned values range between 0 and 1.
23536
0a4c92559a0b Whitespace normalized.
Raymond Hettinger <python@rcn.com>
parents: 23531
diff changeset
657
23531
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
658 """
23536
0a4c92559a0b Whitespace normalized.
Raymond Hettinger <python@rcn.com>
parents: 23531
diff changeset
659
16271
7ef2382f6123 SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents: 16265
diff changeset
660 # 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
661 # 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
662 y = self.gammavariate(alpha, 1.)
7ef2382f6123 SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents: 16265
diff changeset
663 if y == 0:
7ef2382f6123 SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents: 16265
diff changeset
664 return 0.0
7ef2382f6123 SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents: 16265
diff changeset
665 else:
7ef2382f6123 SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents: 16265
diff changeset
666 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
667
16265
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
668 ## -------------------- Pareto --------------------
6698
b73eb4e6face Added Pareto and Weibull distributions, courtesy Jeremy Hylton.
Guido van Rossum <guido@python.org>
parents: 1618
diff changeset
669
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
670 def paretovariate(self, alpha):
23531
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
671 """Pareto distribution. alpha is the shape parameter."""
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
672 # Jain, pg. 495
6698
b73eb4e6face Added Pareto and Weibull distributions, courtesy Jeremy Hylton.
Guido van Rossum <guido@python.org>
parents: 1618
diff changeset
673
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
674 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
675 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
676
16265
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
677 ## -------------------- Weibull --------------------
6698
b73eb4e6face Added Pareto and Weibull distributions, courtesy Jeremy Hylton.
Guido van Rossum <guido@python.org>
parents: 1618
diff changeset
678
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
679 def weibullvariate(self, alpha, beta):
23531
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
680 """Weibull distribution.
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
681
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
682 alpha is the scale parameter and beta is the shape parameter.
23536
0a4c92559a0b Whitespace normalized.
Raymond Hettinger <python@rcn.com>
parents: 23531
diff changeset
683
23531
a98e93eafef7 Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents: 23521
diff changeset
684 """
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
685 # 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
686
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
687 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
688 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
689
16265
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
690 ## -------------------- test program --------------------
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
691
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
692 def _test_generator(n, funccall):
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14617
diff changeset
693 import time
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14617
diff changeset
694 print n, 'times', funccall
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14617
diff changeset
695 code = compile(funccall, funccall, 'eval')
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14617
diff changeset
696 sum = 0.0
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14617
diff changeset
697 sqsum = 0.0
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14617
diff changeset
698 smallest = 1e10
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14617
diff changeset
699 largest = -1e10
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14617
diff changeset
700 t0 = time.time()
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14617
diff changeset
701 for i in range(n):
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14617
diff changeset
702 x = eval(code)
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14617
diff changeset
703 sum = sum + x
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14617
diff changeset
704 sqsum = sqsum + x*x
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14617
diff changeset
705 smallest = min(x, smallest)
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14617
diff changeset
706 largest = max(x, largest)
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14617
diff changeset
707 t1 = time.time()
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14617
diff changeset
708 print round(t1-t0, 3), 'sec,',
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14617
diff changeset
709 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
710 stddev = _sqrt(sqsum/n - avg*avg)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14617
diff changeset
711 print 'avg %g, stddev %g, min %g, max %g' % \
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14617
diff changeset
712 (avg, stddev, smallest, largest)
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
713
23447
08ccb0f3e2d4 Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents: 23442
diff changeset
714 def _test(N=20000):
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
715 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
716 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
717 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
718 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
719 _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
720 _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
721 _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
722 _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
723 _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
724 _test_generator(N, 'vonmisesvariate(0.0, 1.0)')
23447
08ccb0f3e2d4 Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents: 23442
diff changeset
725 _test_generator(N, 'gammavariate(0.01, 1.0)')
08ccb0f3e2d4 Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents: 23442
diff changeset
726 _test_generator(N, 'gammavariate(0.1, 1.0)')
23521
a90faa5f9417 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23451
diff changeset
727 _test_generator(N, 'gammavariate(0.1, 2.0)')
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
728 _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
729 _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
730 _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
731 _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
732 _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
733 _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
734 _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
735 _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
736 _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
737 _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
738
16265
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
739 # Test jumpahead.
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
740 s = getstate()
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
741 jumpahead(N)
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
742 r1 = random()
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
743 # 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
744 setstate(s)
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
745 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
746 random()
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
747 r2 = random()
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
748 if r1 != r2:
d614b6e9fb4e Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents: 16243
diff changeset
749 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
750
16292
0501c2a10315 New comment block to Clarify a subtlety.
Tim Peters <tim.peters@gmail.com>
parents: 16272
diff changeset
751 # 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
752 # 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
753 # 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
754 # 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
755 # 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
756 _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
757 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
758 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
759 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
760 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
761 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
762 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
763 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
764 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
765 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
766 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
767 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
768 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
769 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
770 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
771 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
772 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
773 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
774 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
775 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
776 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
777 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
778 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
779
1575
45728bfecb76 Random variable generators
Guido van Rossum <guido@python.org>
parents:
diff changeset
780 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
781 _test()