Mercurial > cpython-lru_cache-weakref
annotate Lib/random.py @ 25838:8540887bcec6 2.2
Backport of 2.183:
Fix memory leak in add_subclass() found by NealN with valgrind.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 18 Oct 2002 13:42:21 +0000 |
| parents | 276b6c452999 |
| children | 38319409e895 |
| 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 | 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 | 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 |
| 25359 | 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 | 80 __all__ = ["Random","seed","random","uniform","randint","choice", |
| 81 "randrange","shuffle","normalvariate","lognormvariate", | |
| 82 "cunifvariate","expovariate","vonmisesvariate","gammavariate", | |
| 83 "stdgamma","gauss","betavariate","paretovariate","weibullvariate", | |
| 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 | 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: |
|
25581
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
110 """Random number generator base class used by bound module functions. |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
111 |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
112 Used to instantiate instances of Random to get generators that don't |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
113 share state. Especially useful for multi-threaded programs, creating |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
114 a different instance of Random for each thread, and using the jumpahead() |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
115 method to ensure that the generated sequences seen by each thread don't |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
116 overlap. |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
117 |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
118 Class Random can also be subclassed if you want to use a different basic |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
119 generator of your own devising: in that case, override the following |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
120 methods: random(), seed(), getstate(), setstate() and jumpahead(). |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
121 |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
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 | 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 |
|
25358
0fbb76b5074b
Whitespace normalization and backport:
Raymond Hettinger <python@rcn.com>
parents:
23450
diff
changeset
|
166 self.gauss_next = None |
|
0fbb76b5074b
Whitespace normalization and backport:
Raymond Hettinger <python@rcn.com>
parents:
23450
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 |
|
25358
0fbb76b5074b
Whitespace normalization and backport:
Raymond Hettinger <python@rcn.com>
parents:
23450
diff
changeset
|
256 self.gauss_next = None |
|
0fbb76b5074b
Whitespace normalization and backport:
Raymond Hettinger <python@rcn.com>
parents:
23450
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 |
| 25359 | 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()" |
| 25359 | 311 |
| 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()" |
| 25359 | 316 if step == 1 and istart < istop: |
| 317 try: | |
| 318 return istart + int(self.random()*(istop - istart)) | |
| 319 except OverflowError: | |
| 320 # This can happen if istop-istart > sys.maxint + 1, and | |
| 321 # multiplying by random() doesn't reduce it to something | |
| 322 # <= sys.maxint. We know that the overall result fits | |
| 323 # in an int, and can still do it correctly via math.floor(). | |
| 324 # But that adds another function call, so for speed we | |
| 325 # avoided that whenever possible. | |
| 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()" |
| 25359 | 329 |
| 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 | 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 | 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 | 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): |
|
25581
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
387 """Normal distribution. |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
388 |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
389 mu is the mean, and sigma is the standard deviation. |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
390 |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
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 | 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 | 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 | 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): |
|
25581
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
412 """Log normal distribution. |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
413 |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
414 If you take the natural logarithm of this distribution, you'll get a |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
415 normal distribution with mean mu and standard deviation sigma. |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
416 mu can have any value, and sigma must be greater than zero. |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
417 |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
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 | 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 | 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): |
|
25581
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
424 """Circular uniform distribution. |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
425 |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
426 mean is the mean angle, and arc is the range of the distribution, |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
427 centered around the mean angle. Both values must be expressed in |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
428 radians. Returned values range between mean - arc/2 and |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
429 mean + arc/2 and are normalized to between 0 and pi. |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
430 |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
431 Deprecated in version 2.3. Use: |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
432 (mean + arc * (Random.random() - 0.5)) % Math.pi |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
433 |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
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) |
| 1575 | 437 |
|
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
|
438 return (mean + arc * (self.random() - 0.5)) % _pi |
| 1575 | 439 |
|
16265
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
440 ## -------------------- exponential distribution -------------------- |
| 1575 | 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 def expovariate(self, lambd): |
|
25581
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
443 """Exponential distribution. |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
444 |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
445 lambd is 1.0 divided by the desired mean. (The parameter would be |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
446 called "lambda", but that is a reserved word in Python.) Returned |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
447 values range from 0 to positive infinity. |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
448 |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
449 """ |
|
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
|
450 # 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
|
451 # ('lambda' is a Python reserved word) |
| 1575 | 452 |
|
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
|
453 random = self.random |
|
15871
62aeefa01fdd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14617
diff
changeset
|
454 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
|
455 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
|
456 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
|
457 return -_log(u)/lambd |
| 1575 | 458 |
|
16265
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
459 ## -------------------- von Mises distribution -------------------- |
| 1575 | 460 |
|
16242
3c5f09422b69
Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents:
15871
diff
changeset
|
461 def vonmisesvariate(self, mu, kappa): |
|
25581
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
462 """Circular data distribution. |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
463 |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
464 mu is the mean angle, expressed in radians between 0 and 2*pi, and |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
465 kappa is the concentration parameter, which must be greater than or |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
466 equal to zero. If kappa is equal to zero, this distribution reduces |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
467 to a uniform random angle over the range 0 to 2*pi. |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
468 |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
469 """ |
|
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
|
470 # 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
|
471 # 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
|
472 # if kappa = 0 generate uniform random angle |
| 1575 | 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 # 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
|
475 # "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
|
476 # 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
|
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 # 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
|
479 # 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
|
480 |
|
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
|
481 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
|
482 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
|
483 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
|
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 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
|
486 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
|
487 r = (1.0 + b * b)/(2.0 * b) |
| 1575 | 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 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
|
490 u1 = random() |
| 1575 | 491 |
|
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
|
492 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
|
493 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
|
494 c = kappa * (r - f) |
| 1575 | 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 u2 = random() |
| 1575 | 497 |
|
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
|
498 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
|
499 break |
| 1575 | 500 |
|
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
|
501 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
|
502 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
|
503 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
|
504 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
|
505 theta = (mu % TWOPI) - _acos(f) |
| 1575 | 506 |
|
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
|
507 return theta |
| 1575 | 508 |
|
16265
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
509 ## -------------------- gamma distribution -------------------- |
| 1575 | 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 def gammavariate(self, alpha, beta): |
|
25581
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
512 """Gamma distribution. Not the gamma function! |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
513 |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
514 Conditions on the parameters are alpha > 0 and beta > 0. |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
515 |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
516 """ |
| 1575 | 517 |
|
23448
453e59a472b2
Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents:
23443
diff
changeset
|
518 # alpha > 0, beta > 0, mean is alpha*beta, variance is alpha*beta**2 |
|
25358
0fbb76b5074b
Whitespace normalization and backport:
Raymond Hettinger <python@rcn.com>
parents:
23450
diff
changeset
|
519 |
|
23450
1cc93d501b45
Use spaces, not tabs for indents.
Guido van Rossum <guido@python.org>
parents:
23448
diff
changeset
|
520 # Warning: a few older sources define the gamma distribution in terms |
|
1cc93d501b45
Use spaces, not tabs for indents.
Guido van Rossum <guido@python.org>
parents:
23448
diff
changeset
|
521 # of alpha > -1.0 |
|
1cc93d501b45
Use spaces, not tabs for indents.
Guido van Rossum <guido@python.org>
parents:
23448
diff
changeset
|
522 if alpha <= 0.0 or beta <= 0.0: |
|
1cc93d501b45
Use spaces, not tabs for indents.
Guido van Rossum <guido@python.org>
parents:
23448
diff
changeset
|
523 raise ValueError, 'gammavariate: alpha and beta must be > 0.0' |
|
23448
453e59a472b2
Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents:
23443
diff
changeset
|
524 |
|
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
|
525 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
|
526 if alpha > 1.0: |
| 1575 | 527 |
|
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
|
528 # 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
|
529 # 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
|
530 # Applied Statistics, (1977), 26, No. 1, p71-74 |
| 1575 | 531 |
|
23443
03dfe7d343db
Closes SF bug 527139. Moved temp variables inside guard.
Raymond Hettinger <python@rcn.com>
parents:
23136
diff
changeset
|
532 ainv = _sqrt(2.0 * alpha - 1.0) |
|
03dfe7d343db
Closes SF bug 527139. Moved temp variables inside guard.
Raymond Hettinger <python@rcn.com>
parents:
23136
diff
changeset
|
533 bbb = alpha - LOG4 |
|
03dfe7d343db
Closes SF bug 527139. Moved temp variables inside guard.
Raymond Hettinger <python@rcn.com>
parents:
23136
diff
changeset
|
534 ccc = alpha + ainv |
|
25358
0fbb76b5074b
Whitespace normalization and backport:
Raymond Hettinger <python@rcn.com>
parents:
23450
diff
changeset
|
535 |
|
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
|
536 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
|
537 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
|
538 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
|
539 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
|
540 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
|
541 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
|
542 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
|
543 if r + SG_MAGICCONST - 4.5*z >= 0.0 or r >= _log(z): |
|
23448
453e59a472b2
Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents:
23443
diff
changeset
|
544 return x * beta |
| 1575 | 545 |
|
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
|
546 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
|
547 # expovariate(1) |
|
15871
62aeefa01fdd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14617
diff
changeset
|
548 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
|
549 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
|
550 u = random() |
|
23448
453e59a472b2
Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents:
23443
diff
changeset
|
551 return -_log(u) * beta |
| 1575 | 552 |
|
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 else: # alpha is between 0 and 1 (exclusive) |
| 1575 | 554 |
|
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
|
555 # Uses ALGORITHM GS of Statistical Computing - Kennedy & Gentle |
| 1575 | 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 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
|
558 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
|
559 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
|
560 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
|
561 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
|
562 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
|
563 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
|
564 # 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
|
565 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
|
566 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
|
567 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
|
568 ((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
|
569 break |
|
23448
453e59a472b2
Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents:
23443
diff
changeset
|
570 return x * beta |
|
453e59a472b2
Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents:
23443
diff
changeset
|
571 |
|
453e59a472b2
Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents:
23443
diff
changeset
|
572 |
|
453e59a472b2
Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents:
23443
diff
changeset
|
573 def stdgamma(self, alpha, ainv, bbb, ccc): |
|
453e59a472b2
Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents:
23443
diff
changeset
|
574 # This method was (and shall remain) undocumented. |
|
453e59a472b2
Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents:
23443
diff
changeset
|
575 # This method is deprecated |
|
453e59a472b2
Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents:
23443
diff
changeset
|
576 # for the following reasons: |
|
453e59a472b2
Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents:
23443
diff
changeset
|
577 # 1. Returns same as .gammavariate(alpha, 1.0) |
|
453e59a472b2
Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents:
23443
diff
changeset
|
578 # 2. Requires caller to provide 3 extra arguments |
|
453e59a472b2
Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents:
23443
diff
changeset
|
579 # that are functions of alpha anyway |
|
453e59a472b2
Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents:
23443
diff
changeset
|
580 # 3. Can't be used for alpha < 0.5 |
|
453e59a472b2
Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents:
23443
diff
changeset
|
581 |
|
453e59a472b2
Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents:
23443
diff
changeset
|
582 # ainv = sqrt(2 * alpha - 1) |
|
453e59a472b2
Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents:
23443
diff
changeset
|
583 # bbb = alpha - log(4) |
|
453e59a472b2
Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents:
23443
diff
changeset
|
584 # ccc = alpha + ainv |
|
453e59a472b2
Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents:
23443
diff
changeset
|
585 import warnings |
|
453e59a472b2
Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents:
23443
diff
changeset
|
586 warnings.warn("The stdgamma function is deprecated; " |
|
453e59a472b2
Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents:
23443
diff
changeset
|
587 "use gammavariate() instead", |
|
453e59a472b2
Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents:
23443
diff
changeset
|
588 DeprecationWarning) |
|
453e59a472b2
Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents:
23443
diff
changeset
|
589 return self.gammavariate(alpha, 1.0) |
|
453e59a472b2
Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents:
23443
diff
changeset
|
590 |
| 1575 | 591 |
|
1576
695fb494a3a2
Added gauss() (same as normal but twice as fast) and betavariate();
Guido van Rossum <guido@python.org>
parents:
1575
diff
changeset
|
592 |
|
16265
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
593 ## -------------------- 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
|
594 |
|
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
|
595 def gauss(self, mu, sigma): |
|
25581
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
596 """Gaussian distribution. |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
597 |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
598 mu is the mean, and sigma is the standard deviation. This is |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
599 slightly faster than the normalvariate() function. |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
600 |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
601 Not thread-safe without a lock around calls. |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
602 |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
603 """ |
|
1577
444e349f962f
Use float constants directly; cosmetic changes; initialize largest
Guido van Rossum <guido@python.org>
parents:
1576
diff
changeset
|
604 |
|
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
|
605 # 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
|
606 # 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
|
607 # |
|
3c5f09422b69
Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents:
15871
diff
changeset
|
608 # 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
|
609 # 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
|
610 # |
|
3c5f09422b69
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 # 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
|
612 # (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
|
613 # (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
|
614 # (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
|
615 |
|
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
|
616 # 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
|
617 # 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
|
618 # 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
|
619 # 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
|
620 # 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
|
621 # lock here.) |
|
1577
444e349f962f
Use float constants directly; cosmetic changes; initialize largest
Guido van Rossum <guido@python.org>
parents:
1576
diff
changeset
|
622 |
|
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
|
623 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
|
624 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
|
625 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
|
626 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
|
627 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
|
628 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
|
629 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
|
630 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
|
631 |
|
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
|
632 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
|
633 |
|
16265
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
634 ## -------------------- beta -------------------- |
|
16271
7ef2382f6123
SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents:
16265
diff
changeset
|
635 ## See |
|
7ef2382f6123
SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents:
16265
diff
changeset
|
636 ## 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
|
637 ## 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
|
638 ## |
|
7ef2382f6123
SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents:
16265
diff
changeset
|
639 ## def betavariate(self, alpha, beta): |
|
7ef2382f6123
SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents:
16265
diff
changeset
|
640 ## # 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
|
641 ## |
|
7ef2382f6123
SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents:
16265
diff
changeset
|
642 ## y = self.expovariate(alpha) |
|
7ef2382f6123
SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents:
16265
diff
changeset
|
643 ## 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
|
644 ## return z/(y+z) |
|
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 ## 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
|
647 |
|
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
|
648 def betavariate(self, alpha, beta): |
|
25581
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
649 """Beta distribution. |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
650 |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
651 Conditions on the parameters are alpha > -1 and beta} > -1. |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
652 Returned values range between 0 and 1. |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
653 |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
654 """ |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
655 |
|
16271
7ef2382f6123
SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents:
16265
diff
changeset
|
656 # 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
|
657 # 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
|
658 y = self.gammavariate(alpha, 1.) |
|
7ef2382f6123
SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents:
16265
diff
changeset
|
659 if y == 0: |
|
7ef2382f6123
SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents:
16265
diff
changeset
|
660 return 0.0 |
|
7ef2382f6123
SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents:
16265
diff
changeset
|
661 else: |
|
7ef2382f6123
SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents:
16265
diff
changeset
|
662 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
|
663 |
|
16265
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
664 ## -------------------- Pareto -------------------- |
|
6698
b73eb4e6face
Added Pareto and Weibull distributions, courtesy Jeremy Hylton.
Guido van Rossum <guido@python.org>
parents:
1618
diff
changeset
|
665 |
|
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
|
666 def paretovariate(self, alpha): |
|
25581
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
667 """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
|
668 # Jain, pg. 495 |
|
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 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
|
671 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
|
672 |
|
16265
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
673 ## -------------------- Weibull -------------------- |
|
6698
b73eb4e6face
Added Pareto and Weibull distributions, courtesy Jeremy Hylton.
Guido van Rossum <guido@python.org>
parents:
1618
diff
changeset
|
674 |
|
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
|
675 def weibullvariate(self, alpha, beta): |
|
25581
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
676 """Weibull distribution. |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
677 |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
678 alpha is the scale parameter and beta is the shape parameter. |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
679 |
|
276b6c452999
Backport 1.33 (omitting the deprecation warning but keeping the deprecation
Raymond Hettinger <python@rcn.com>
parents:
25359
diff
changeset
|
680 """ |
|
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
|
681 # 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
|
682 |
|
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
|
683 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
|
684 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
|
685 |
|
16265
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
686 ## -------------------- test program -------------------- |
| 1575 | 687 |
|
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
|
688 def _test_generator(n, funccall): |
|
15871
62aeefa01fdd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14617
diff
changeset
|
689 import time |
|
62aeefa01fdd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14617
diff
changeset
|
690 print n, 'times', funccall |
|
62aeefa01fdd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14617
diff
changeset
|
691 code = compile(funccall, funccall, 'eval') |
|
62aeefa01fdd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14617
diff
changeset
|
692 sum = 0.0 |
|
62aeefa01fdd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14617
diff
changeset
|
693 sqsum = 0.0 |
|
62aeefa01fdd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14617
diff
changeset
|
694 smallest = 1e10 |
|
62aeefa01fdd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14617
diff
changeset
|
695 largest = -1e10 |
|
62aeefa01fdd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14617
diff
changeset
|
696 t0 = time.time() |
|
62aeefa01fdd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14617
diff
changeset
|
697 for i in range(n): |
|
62aeefa01fdd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14617
diff
changeset
|
698 x = eval(code) |
|
62aeefa01fdd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14617
diff
changeset
|
699 sum = sum + x |
|
62aeefa01fdd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14617
diff
changeset
|
700 sqsum = sqsum + x*x |
|
62aeefa01fdd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14617
diff
changeset
|
701 smallest = min(x, smallest) |
|
62aeefa01fdd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14617
diff
changeset
|
702 largest = max(x, largest) |
|
62aeefa01fdd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14617
diff
changeset
|
703 t1 = time.time() |
|
62aeefa01fdd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14617
diff
changeset
|
704 print round(t1-t0, 3), 'sec,', |
|
62aeefa01fdd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14617
diff
changeset
|
705 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
|
706 stddev = _sqrt(sqsum/n - avg*avg) |
|
15871
62aeefa01fdd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14617
diff
changeset
|
707 print 'avg %g, stddev %g, min %g, max %g' % \ |
|
62aeefa01fdd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14617
diff
changeset
|
708 (avg, stddev, smallest, largest) |
| 1575 | 709 |
|
23448
453e59a472b2
Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents:
23443
diff
changeset
|
710 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
|
711 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
|
712 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
|
713 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
|
714 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
|
715 _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
|
716 _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
|
717 _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
|
718 _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
|
719 _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
|
720 _test_generator(N, 'vonmisesvariate(0.0, 1.0)') |
|
23448
453e59a472b2
Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents:
23443
diff
changeset
|
721 _test_generator(N, 'gammavariate(0.01, 1.0)') |
|
453e59a472b2
Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents:
23443
diff
changeset
|
722 _test_generator(N, 'gammavariate(0.1, 1.0)') |
|
25358
0fbb76b5074b
Whitespace normalization and backport:
Raymond Hettinger <python@rcn.com>
parents:
23450
diff
changeset
|
723 _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
|
724 _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
|
725 _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
|
726 _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
|
727 _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
|
728 _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
|
729 _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
|
730 _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
|
731 _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
|
732 _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
|
733 _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
|
734 |
|
16265
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
735 # Test jumpahead. |
|
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
736 s = getstate() |
|
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
737 jumpahead(N) |
|
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
738 r1 = random() |
|
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
739 # 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
|
740 setstate(s) |
|
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
741 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
|
742 random() |
|
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
743 r2 = random() |
|
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
744 if r1 != r2: |
|
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
745 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
|
746 |
|
16292
0501c2a10315
New comment block to Clarify a subtlety.
Tim Peters <tim.peters@gmail.com>
parents:
16272
diff
changeset
|
747 # 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
|
748 # 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
|
749 # 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
|
750 # 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
|
751 # 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
|
752 _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
|
753 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
|
754 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
|
755 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
|
756 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
|
757 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
|
758 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
|
759 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
|
760 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
|
761 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
|
762 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
|
763 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
|
764 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
|
765 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
|
766 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
|
767 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
|
768 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
|
769 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
|
770 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
|
771 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
|
772 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
|
773 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
|
774 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
|
775 |
| 1575 | 776 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
|
777 _test() |
