Mercurial > cpython-lru_cache-weakref
annotate Lib/random.py @ 74135:9c19df6c8ea0 3.2
News entry for Issue12798
| author | Senthil Kumaran <senthil@uthcode.com> |
|---|---|
| date | Fri, 23 Dec 2011 11:11:21 +0800 |
| parents | 3fdc5a75d6e1 |
| children | e2739145657d 4ef224fbf34e |
| 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 |
|
26155
8ceb455f5541
SF patch 629637: Add sample(population, k) method to the random module.
Raymond Hettinger <python@rcn.com>
parents:
24861
diff
changeset
|
10 pick random sample |
|
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
|
11 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
|
12 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11200
diff
changeset
|
13 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
|
14 ------------------------------ |
|
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
|
15 uniform |
|
46034
0a485593ae1c
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44665
diff
changeset
|
16 triangular |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11200
diff
changeset
|
17 normal (Gaussian) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11200
diff
changeset
|
18 lognormal |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11200
diff
changeset
|
19 negative exponential |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11200
diff
changeset
|
20 gamma |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11200
diff
changeset
|
21 beta |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
26564
diff
changeset
|
22 pareto |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
26564
diff
changeset
|
23 Weibull |
| 1575 | 24 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11200
diff
changeset
|
25 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
|
26 --------------------------------------------- |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11200
diff
changeset
|
27 circular uniform |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11200
diff
changeset
|
28 von Mises |
| 1575 | 29 |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
26564
diff
changeset
|
30 General notes on the underlying Mersenne Twister core generator: |
|
16272
835e9dd54bfb
The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents:
16271
diff
changeset
|
31 |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
26564
diff
changeset
|
32 * The period is 2**19937-1. |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
33 * It is one of the most extensively tested generators in existence. |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
34 * The random() method is implemented in C, executes in a single Python step, |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
35 and is, therefore, threadsafe. |
|
16272
835e9dd54bfb
The combo of getstate/setstate/jumpahead is very powerful, but needs
Tim Peters <tim.peters@gmail.com>
parents:
16271
diff
changeset
|
36 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
11200
diff
changeset
|
37 """ |
|
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
|
38 |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
39 from warnings import warn as _warn |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
40 from types import MethodType as _MethodType, BuiltinMethodType as _BuiltinMethodType |
|
35359
c5bf5685267b
Implement random.sample() using sets instead of dicts.
Raymond Hettinger <python@rcn.com>
parents:
34942
diff
changeset
|
41 from math import log as _log, exp as _exp, pi as _pi, e as _e, ceil as _ceil |
|
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
|
42 from math import sqrt as _sqrt, acos as _acos, cos as _cos, sin as _sin |
|
33409
8e23bd3d5b1a
Fulfill Martin's request to use try/except rather than a "look before
Raymond Hettinger <python@rcn.com>
parents:
33399
diff
changeset
|
43 from os import urandom as _urandom |
|
69859
e2f7db1255df
Sync-up minor code edits with the default branch.
Raymond Hettinger <python@rcn.com>
parents:
69857
diff
changeset
|
44 from collections import Set as _Set, Sequence as _Sequence |
|
66786
f4f56efa2870
Update whatsnew. Salt the random number seed.
Raymond Hettinger <python@rcn.com>
parents:
64569
diff
changeset
|
45 from hashlib import sha512 as _sha512 |
|
8455
af9273b1073a
Make gauss() semi-thread-safe. It can still give duplicate results,
Guido van Rossum <guido@python.org>
parents:
8393
diff
changeset
|
46 |
|
26155
8ceb455f5541
SF patch 629637: Add sample(population, k) method to the random module.
Raymond Hettinger <python@rcn.com>
parents:
24861
diff
changeset
|
47 __all__ = ["Random","seed","random","uniform","randint","choice","sample", |
| 16613 | 48 "randrange","shuffle","normalvariate","lognormvariate", |
|
46034
0a485593ae1c
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44665
diff
changeset
|
49 "expovariate","vonmisesvariate","gammavariate","triangular", |
|
30068
171814f5dcc6
Removed deprecated functions
Raymond Hettinger <python@rcn.com>
parents:
29586
diff
changeset
|
50 "gauss","betavariate","paretovariate","weibullvariate", |
|
44662
c25760c21397
Remove defunct parts of the random module
Raymond Hettinger <python@rcn.com>
parents:
44129
diff
changeset
|
51 "getstate","setstate", "getrandbits", |
|
33481
eacdc29606d8
SF #1027105: HardwareRandom should be renamed OSRandom
Raymond Hettinger <python@rcn.com>
parents:
33409
diff
changeset
|
52 "SystemRandom"] |
| 1575 | 53 |
|
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
|
54 NV_MAGICCONST = 4 * _exp(-0.5)/_sqrt(2.0) |
|
3c5f09422b69
Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents:
15871
diff
changeset
|
55 TWOPI = 2.0*_pi |
|
3c5f09422b69
Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents:
15871
diff
changeset
|
56 LOG4 = _log(4.0) |
|
3c5f09422b69
Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents:
15871
diff
changeset
|
57 SG_MAGICCONST = 1.0 + _log(4.5) |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
58 BPF = 53 # Number of bits in a float |
|
33350
c44f083465f9
HardwareRandom: Go back to multiplying by 2**-BPF instead of using
Tim Peters <tim.peters@gmail.com>
parents:
33349
diff
changeset
|
59 RECIP_BPF = 2**-BPF |
|
8393
7669ddf0e7d4
Add Interfaces to replace remaining needs for importing whrandom.
Guido van Rossum <guido@python.org>
parents:
7881
diff
changeset
|
60 |
|
33329
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32287
diff
changeset
|
61 |
|
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
|
62 # Translated by Guido van Rossum from C source provided by |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
26564
diff
changeset
|
63 # Adrian Baddeley. Adapted by Raymond Hettinger for use with |
|
33349
ddba154c0e00
Take advantage of the math library's ldexp for assembling a float by
Raymond Hettinger <python@rcn.com>
parents:
33329
diff
changeset
|
64 # the Mersenne Twister and os.urandom() core generators. |
|
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
|
65 |
|
27131
e87d590a5de1
Much clearer when super() is used.
Raymond Hettinger <python@rcn.com>
parents:
27094
diff
changeset
|
66 import _random |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
26564
diff
changeset
|
67 |
|
27131
e87d590a5de1
Much clearer when super() is used.
Raymond Hettinger <python@rcn.com>
parents:
27094
diff
changeset
|
68 class Random(_random.Random): |
|
23531
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
69 """Random number generator base class used by bound module functions. |
|
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
70 |
|
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
71 Used to instantiate instances of Random to get generators that don't |
|
44662
c25760c21397
Remove defunct parts of the random module
Raymond Hettinger <python@rcn.com>
parents:
44129
diff
changeset
|
72 share state. |
|
23531
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
73 |
|
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
74 Class Random can also be subclassed if you want to use a different basic |
|
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
75 generator of your own devising: in that case, override the following |
|
44662
c25760c21397
Remove defunct parts of the random module
Raymond Hettinger <python@rcn.com>
parents:
44129
diff
changeset
|
76 methods: random(), seed(), getstate(), and setstate(). |
|
48596
13b2e11f3cf5
Merged revisions 65259,65263,65296,65307,65321 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
46034
diff
changeset
|
77 Optionally, implement a getrandbits() method so that randrange() |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
78 can cover arbitrarily large ranges. |
|
23536
0a4c92559a0b
Whitespace normalized.
Raymond Hettinger <python@rcn.com>
parents:
23531
diff
changeset
|
79 |
|
23531
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
80 """ |
|
8393
7669ddf0e7d4
Add Interfaces to replace remaining needs for importing whrandom.
Guido van Rossum <guido@python.org>
parents:
7881
diff
changeset
|
81 |
|
44129
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43020
diff
changeset
|
82 VERSION = 3 # used by getstate/setstate |
|
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
|
83 |
|
3c5f09422b69
Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents:
15871
diff
changeset
|
84 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
|
85 """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
|
86 |
|
3c5f09422b69
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 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
|
88 """ |
|
3c5f09422b69
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 |
|
3c5f09422b69
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 self.seed(x) |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
26564
diff
changeset
|
91 self.gauss_next = 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
|
92 |
|
64510
65642f085c55
Document which part of the random module module are guaranteed.
Raymond Hettinger <python@rcn.com>
parents:
57584
diff
changeset
|
93 def seed(self, a=None, version=2): |
|
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
|
94 """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
|
95 |
|
33481
eacdc29606d8
SF #1027105: HardwareRandom should be renamed OSRandom
Raymond Hettinger <python@rcn.com>
parents:
33409
diff
changeset
|
96 None or no argument seeds from current time or from an operating |
|
eacdc29606d8
SF #1027105: HardwareRandom should be renamed OSRandom
Raymond Hettinger <python@rcn.com>
parents:
33409
diff
changeset
|
97 system specific randomness source if available. |
|
8393
7669ddf0e7d4
Add Interfaces to replace remaining needs for importing whrandom.
Guido van Rossum <guido@python.org>
parents:
7881
diff
changeset
|
98 |
|
64567
b1865ca7b65c
* Remove dependency on binascii.hexlify by using int.from_bytes().
Raymond Hettinger <python@rcn.com>
parents:
64553
diff
changeset
|
99 For version 2 (the default), all of the bits are used if *a *is a str, |
|
b1865ca7b65c
* Remove dependency on binascii.hexlify by using int.from_bytes().
Raymond Hettinger <python@rcn.com>
parents:
64553
diff
changeset
|
100 bytes, or bytearray. For version 1, the hash() of *a* is used instead. |
|
64510
65642f085c55
Document which part of the random module module are guaranteed.
Raymond Hettinger <python@rcn.com>
parents:
57584
diff
changeset
|
101 |
|
64567
b1865ca7b65c
* Remove dependency on binascii.hexlify by using int.from_bytes().
Raymond Hettinger <python@rcn.com>
parents:
64553
diff
changeset
|
102 If *a* is an int, all bits are used. |
|
64510
65642f085c55
Document which part of the random module module are guaranteed.
Raymond Hettinger <python@rcn.com>
parents:
57584
diff
changeset
|
103 |
|
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
|
104 """ |
|
8393
7669ddf0e7d4
Add Interfaces to replace remaining needs for importing whrandom.
Guido van Rossum <guido@python.org>
parents:
7881
diff
changeset
|
105 |
|
30114
9f6e2dee1892
SF bug #778964: bad seed in python 2.3 random
Raymond Hettinger <python@rcn.com>
parents:
30068
diff
changeset
|
106 if a is None: |
|
33409
8e23bd3d5b1a
Fulfill Martin's request to use try/except rather than a "look before
Raymond Hettinger <python@rcn.com>
parents:
33399
diff
changeset
|
107 try: |
|
64567
b1865ca7b65c
* Remove dependency on binascii.hexlify by using int.from_bytes().
Raymond Hettinger <python@rcn.com>
parents:
64553
diff
changeset
|
108 a = int.from_bytes(_urandom(32), 'big') |
|
33409
8e23bd3d5b1a
Fulfill Martin's request to use try/except rather than a "look before
Raymond Hettinger <python@rcn.com>
parents:
33399
diff
changeset
|
109 except NotImplementedError: |
|
33329
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32287
diff
changeset
|
110 import time |
|
40644
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
111 a = int(time.time() * 256) # use fractional seconds |
|
33329
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32287
diff
changeset
|
112 |
|
66786
f4f56efa2870
Update whatsnew. Salt the random number seed.
Raymond Hettinger <python@rcn.com>
parents:
64569
diff
changeset
|
113 if version == 2: |
|
f4f56efa2870
Update whatsnew. Salt the random number seed.
Raymond Hettinger <python@rcn.com>
parents:
64569
diff
changeset
|
114 if isinstance(a, (str, bytes, bytearray)): |
|
f4f56efa2870
Update whatsnew. Salt the random number seed.
Raymond Hettinger <python@rcn.com>
parents:
64569
diff
changeset
|
115 if isinstance(a, str): |
|
69857
1a56775c6e54
Avoid codec spelling issues by just using the utf-8 default.
Raymond Hettinger <python@rcn.com>
parents:
69363
diff
changeset
|
116 a = a.encode() |
|
66786
f4f56efa2870
Update whatsnew. Salt the random number seed.
Raymond Hettinger <python@rcn.com>
parents:
64569
diff
changeset
|
117 a += _sha512(a).digest() |
|
f4f56efa2870
Update whatsnew. Salt the random number seed.
Raymond Hettinger <python@rcn.com>
parents:
64569
diff
changeset
|
118 a = int.from_bytes(a, 'big') |
|
64510
65642f085c55
Document which part of the random module module are guaranteed.
Raymond Hettinger <python@rcn.com>
parents:
57584
diff
changeset
|
119 |
|
42084
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41642
diff
changeset
|
120 super().seed(a) |
|
23380
9be01b7b6c80
random.gauss() uses a piece of hidden state used by nothing else,
Tim Peters <tim.peters@gmail.com>
parents:
23023
diff
changeset
|
121 self.gauss_next = None |
|
9be01b7b6c80
random.gauss() uses a piece of hidden state used by nothing else,
Tim Peters <tim.peters@gmail.com>
parents:
23023
diff
changeset
|
122 |
|
16265
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
123 def getstate(self): |
|
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
124 """Return internal state; can be passed to setstate() later.""" |
|
42084
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41642
diff
changeset
|
125 return self.VERSION, super().getstate(), self.gauss_next |
|
16265
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
126 |
|
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
127 def setstate(self, state): |
|
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
128 """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
|
129 version = state[0] |
|
44129
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43020
diff
changeset
|
130 if version == 3: |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
26564
diff
changeset
|
131 version, internalstate, self.gauss_next = state |
|
42084
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41642
diff
changeset
|
132 super().setstate(internalstate) |
|
44129
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43020
diff
changeset
|
133 elif version == 2: |
|
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43020
diff
changeset
|
134 version, internalstate, self.gauss_next = state |
|
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43020
diff
changeset
|
135 # In version 2, the state was saved as signed ints, which causes |
|
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43020
diff
changeset
|
136 # inconsistencies between 32/64-bit systems. The state is |
|
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43020
diff
changeset
|
137 # really unsigned 32-bit ints, so we convert negative ints from |
|
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43020
diff
changeset
|
138 # version 2 to positive longs for version 3. |
|
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43020
diff
changeset
|
139 try: |
|
64523
6095ea9dcff4
Fix test that depends on a particular implementation of random.choice().
Raymond Hettinger <python@rcn.com>
parents:
64517
diff
changeset
|
140 internalstate = tuple(x % (2**32) for x in internalstate) |
|
44129
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43020
diff
changeset
|
141 except ValueError as e: |
|
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43020
diff
changeset
|
142 raise TypeError from e |
|
64567
b1865ca7b65c
* Remove dependency on binascii.hexlify by using int.from_bytes().
Raymond Hettinger <python@rcn.com>
parents:
64553
diff
changeset
|
143 super().setstate(internalstate) |
|
16265
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
144 else: |
|
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
145 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
|
146 "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
|
147 (version, self.VERSION)) |
|
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
148 |
|
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
149 ## ---- 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
|
150 ## ---- 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
|
151 |
|
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
152 ## -------------------- pickle support ------------------- |
|
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
153 |
|
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
154 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
|
155 return self.getstate() |
|
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
156 |
|
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
157 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
|
158 self.setstate(state) |
|
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
159 |
|
29586
8b163370372d
SF bug #759889: Pickling of Random is broken
Raymond Hettinger <python@rcn.com>
parents:
29539
diff
changeset
|
160 def __reduce__(self): |
|
8b163370372d
SF bug #759889: Pickling of Random is broken
Raymond Hettinger <python@rcn.com>
parents:
29539
diff
changeset
|
161 return self.__class__, (), self.getstate() |
|
8b163370372d
SF bug #759889: Pickling of Random is broken
Raymond Hettinger <python@rcn.com>
parents:
29539
diff
changeset
|
162 |
|
16265
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
163 ## -------------------- integer methods ------------------- |
|
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
164 |
|
64512
c2f8418a0e14
Issues #7889, #9025 and #9379: Improvements to the random module.
Raymond Hettinger <python@rcn.com>
parents:
64511
diff
changeset
|
165 def randrange(self, start, stop=None, step=1, int=int): |
|
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
|
166 """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
|
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 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
|
169 endpoint; in Python this is usually not what you want. |
| 64511 | 170 |
| 64516 | 171 Do not supply the 'int' argument. |
|
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
|
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 |
|
3c5f09422b69
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 # This code is a bit messy to make it fast for the |
|
24861
4e795fe9f54b
SF bug 594996: OverflowError in random.randrange
Tim Peters <tim.peters@gmail.com>
parents:
23536
diff
changeset
|
175 # 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
|
176 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
|
177 if istart != start: |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42084
diff
changeset
|
178 raise ValueError("non-integer arg 1 for randrange()") |
| 64511 | 179 if stop 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
|
180 if istart > 0: |
|
64512
c2f8418a0e14
Issues #7889, #9025 and #9379: Improvements to the random module.
Raymond Hettinger <python@rcn.com>
parents:
64511
diff
changeset
|
181 return self._randbelow(istart) |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42084
diff
changeset
|
182 raise ValueError("empty range for randrange()") |
|
24861
4e795fe9f54b
SF bug 594996: OverflowError in random.randrange
Tim Peters <tim.peters@gmail.com>
parents:
23536
diff
changeset
|
183 |
|
4e795fe9f54b
SF bug 594996: OverflowError in random.randrange
Tim Peters <tim.peters@gmail.com>
parents:
23536
diff
changeset
|
184 # 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
|
185 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
|
186 if istop != stop: |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42084
diff
changeset
|
187 raise ValueError("non-integer stop for randrange()") |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
188 width = istop - istart |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
189 if step == 1 and width > 0: |
| 64516 | 190 return istart + self._randbelow(width) |
|
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
|
191 if step == 1: |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42084
diff
changeset
|
192 raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width)) |
|
24861
4e795fe9f54b
SF bug 594996: OverflowError in random.randrange
Tim Peters <tim.peters@gmail.com>
parents:
23536
diff
changeset
|
193 |
|
4e795fe9f54b
SF bug 594996: OverflowError in random.randrange
Tim Peters <tim.peters@gmail.com>
parents:
23536
diff
changeset
|
194 # 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
|
195 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
|
196 if istep != step: |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42084
diff
changeset
|
197 raise ValueError("non-integer step for randrange()") |
|
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
|
198 if istep > 0: |
|
33597
355b15e4a4da
Use floor division operator.
Raymond Hettinger <python@rcn.com>
parents:
33481
diff
changeset
|
199 n = (width + istep - 1) // istep |
|
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
|
200 elif istep < 0: |
|
33597
355b15e4a4da
Use floor division operator.
Raymond Hettinger <python@rcn.com>
parents:
33481
diff
changeset
|
201 n = (width + istep + 1) // istep |
|
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
|
202 else: |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42084
diff
changeset
|
203 raise ValueError("zero step for randrange()") |
|
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
|
204 |
|
3c5f09422b69
Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents:
15871
diff
changeset
|
205 if n <= 0: |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42084
diff
changeset
|
206 raise ValueError("empty range for randrange()") |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
207 |
|
64512
c2f8418a0e14
Issues #7889, #9025 and #9379: Improvements to the random module.
Raymond Hettinger <python@rcn.com>
parents:
64511
diff
changeset
|
208 return istart + istep*self._randbelow(n) |
| 1575 | 209 |
|
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
|
210 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
|
211 """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
|
212 """ |
|
3c5f09422b69
Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents:
15871
diff
changeset
|
213 |
|
3c5f09422b69
Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents:
15871
diff
changeset
|
214 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
|
215 |
|
64553
d5331712b509
In the case where only a user supplied random() method is available,
Raymond Hettinger <python@rcn.com>
parents:
64532
diff
changeset
|
216 def _randbelow(self, n, int=int, maxsize=1<<BPF, type=type, |
|
64531
770c3ec05685
Minor refactoring and cleanup. Extend looping randrange() technique to subclasses.
Raymond Hettinger <python@rcn.com>
parents:
64523
diff
changeset
|
217 Method=_MethodType, BuiltinMethod=_BuiltinMethodType): |
|
64553
d5331712b509
In the case where only a user supplied random() method is available,
Raymond Hettinger <python@rcn.com>
parents:
64532
diff
changeset
|
218 "Return a random int in the range [0,n). Raises ValueError if n==0." |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
219 |
| 64516 | 220 getrandbits = self.getrandbits |
| 221 # Only call self.getrandbits if the original random() builtin method | |
| 222 # has not been overridden or if a new getrandbits() was supplied. | |
|
64531
770c3ec05685
Minor refactoring and cleanup. Extend looping randrange() technique to subclasses.
Raymond Hettinger <python@rcn.com>
parents:
64523
diff
changeset
|
223 if type(self.random) is BuiltinMethod or type(getrandbits) is Method: |
|
64553
d5331712b509
In the case where only a user supplied random() method is available,
Raymond Hettinger <python@rcn.com>
parents:
64532
diff
changeset
|
224 k = n.bit_length() # don't use (n-1) here because n can be 1 |
|
64532
89fe5179aec5
Neaten-up comments and warning message.
Raymond Hettinger <python@rcn.com>
parents:
64531
diff
changeset
|
225 r = getrandbits(k) # 0 <= r < 2**k |
| 64516 | 226 while r >= n: |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
227 r = getrandbits(k) |
| 64516 | 228 return r |
|
64531
770c3ec05685
Minor refactoring and cleanup. Extend looping randrange() technique to subclasses.
Raymond Hettinger <python@rcn.com>
parents:
64523
diff
changeset
|
229 # There's an overriden random() method but no new getrandbits() method, |
|
770c3ec05685
Minor refactoring and cleanup. Extend looping randrange() technique to subclasses.
Raymond Hettinger <python@rcn.com>
parents:
64523
diff
changeset
|
230 # so we can only use random() from here. |
|
64553
d5331712b509
In the case where only a user supplied random() method is available,
Raymond Hettinger <python@rcn.com>
parents:
64532
diff
changeset
|
231 random = self.random |
|
d5331712b509
In the case where only a user supplied random() method is available,
Raymond Hettinger <python@rcn.com>
parents:
64532
diff
changeset
|
232 if n >= maxsize: |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
233 _warn("Underlying random() generator does not supply \n" |
|
64532
89fe5179aec5
Neaten-up comments and warning message.
Raymond Hettinger <python@rcn.com>
parents:
64531
diff
changeset
|
234 "enough bits to choose from a population range this large.\n" |
|
89fe5179aec5
Neaten-up comments and warning message.
Raymond Hettinger <python@rcn.com>
parents:
64531
diff
changeset
|
235 "To remove the range limitation, add a getrandbits() method.") |
|
64553
d5331712b509
In the case where only a user supplied random() method is available,
Raymond Hettinger <python@rcn.com>
parents:
64532
diff
changeset
|
236 return int(random() * n) |
|
d5331712b509
In the case where only a user supplied random() method is available,
Raymond Hettinger <python@rcn.com>
parents:
64532
diff
changeset
|
237 rem = maxsize % n |
|
d5331712b509
In the case where only a user supplied random() method is available,
Raymond Hettinger <python@rcn.com>
parents:
64532
diff
changeset
|
238 limit = (maxsize - rem) / maxsize # int(limit * maxsize) % n == 0 |
|
d5331712b509
In the case where only a user supplied random() method is available,
Raymond Hettinger <python@rcn.com>
parents:
64532
diff
changeset
|
239 r = random() |
|
d5331712b509
In the case where only a user supplied random() method is available,
Raymond Hettinger <python@rcn.com>
parents:
64532
diff
changeset
|
240 while r >= limit: |
|
d5331712b509
In the case where only a user supplied random() method is available,
Raymond Hettinger <python@rcn.com>
parents:
64532
diff
changeset
|
241 r = random() |
|
d5331712b509
In the case where only a user supplied random() method is available,
Raymond Hettinger <python@rcn.com>
parents:
64532
diff
changeset
|
242 return int(r*maxsize) % n |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
243 |
|
16265
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
244 ## -------------------- sequence methods ------------------- |
|
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
245 |
|
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
|
246 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
|
247 """Choose a random element from a non-empty sequence.""" |
|
64517
e83af141dffd
Fix corner case for Random.choice() and add tests.
Raymond Hettinger <python@rcn.com>
parents:
64516
diff
changeset
|
248 try: |
|
e83af141dffd
Fix corner case for Random.choice() and add tests.
Raymond Hettinger <python@rcn.com>
parents:
64516
diff
changeset
|
249 i = self._randbelow(len(seq)) |
|
e83af141dffd
Fix corner case for Random.choice() and add tests.
Raymond Hettinger <python@rcn.com>
parents:
64516
diff
changeset
|
250 except ValueError: |
|
e83af141dffd
Fix corner case for Random.choice() and add tests.
Raymond Hettinger <python@rcn.com>
parents:
64516
diff
changeset
|
251 raise IndexError('Cannot choose from an empty sequence') |
|
e83af141dffd
Fix corner case for Random.choice() and add tests.
Raymond Hettinger <python@rcn.com>
parents:
64516
diff
changeset
|
252 return seq[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
|
253 |
|
3c5f09422b69
Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents:
15871
diff
changeset
|
254 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
|
255 """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
|
256 |
|
3c5f09422b69
Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents:
15871
diff
changeset
|
257 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
|
258 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
|
259 """ |
|
3c5f09422b69
Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents:
15871
diff
changeset
|
260 |
|
64531
770c3ec05685
Minor refactoring and cleanup. Extend looping randrange() technique to subclasses.
Raymond Hettinger <python@rcn.com>
parents:
64523
diff
changeset
|
261 randbelow = self._randbelow |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41592
diff
changeset
|
262 for i in reversed(range(1, len(x))): |
|
16265
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
263 # pick an element in x[:i+1] with which to exchange x[i] |
|
64531
770c3ec05685
Minor refactoring and cleanup. Extend looping randrange() technique to subclasses.
Raymond Hettinger <python@rcn.com>
parents:
64523
diff
changeset
|
264 j = randbelow(i+1) if random is None else int(random() * (i+1)) |
|
16242
3c5f09422b69
Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents:
15871
diff
changeset
|
265 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
|
266 |
|
29430
b80a22250e4f
SF bug #753602: random.sample not properly documented
Raymond Hettinger <python@rcn.com>
parents:
29250
diff
changeset
|
267 def sample(self, population, k): |
|
44665
91928d99ebb0
Take Tim's advice and have random.sample() support only sequences and sets.
Raymond Hettinger <python@rcn.com>
parents:
44662
diff
changeset
|
268 """Chooses k unique random elements from a population sequence or set. |
|
26155
8ceb455f5541
SF patch 629637: Add sample(population, k) method to the random module.
Raymond Hettinger <python@rcn.com>
parents:
24861
diff
changeset
|
269 |
|
26178
83ae4a902df0
Improved clarity and thoroughness of docstring.
Raymond Hettinger <python@rcn.com>
parents:
26172
diff
changeset
|
270 Returns a new list containing elements from the population while |
|
83ae4a902df0
Improved clarity and thoroughness of docstring.
Raymond Hettinger <python@rcn.com>
parents:
26172
diff
changeset
|
271 leaving the original population unchanged. The resulting list is |
|
83ae4a902df0
Improved clarity and thoroughness of docstring.
Raymond Hettinger <python@rcn.com>
parents:
26172
diff
changeset
|
272 in selection order so that all sub-slices will also be valid random |
|
83ae4a902df0
Improved clarity and thoroughness of docstring.
Raymond Hettinger <python@rcn.com>
parents:
26172
diff
changeset
|
273 samples. This allows raffle winners (the sample) to be partitioned |
|
83ae4a902df0
Improved clarity and thoroughness of docstring.
Raymond Hettinger <python@rcn.com>
parents:
26172
diff
changeset
|
274 into grand prize and second place winners (the subslices). |
|
26155
8ceb455f5541
SF patch 629637: Add sample(population, k) method to the random module.
Raymond Hettinger <python@rcn.com>
parents:
24861
diff
changeset
|
275 |
|
26178
83ae4a902df0
Improved clarity and thoroughness of docstring.
Raymond Hettinger <python@rcn.com>
parents:
26172
diff
changeset
|
276 Members of the population need not be hashable or unique. If the |
|
83ae4a902df0
Improved clarity and thoroughness of docstring.
Raymond Hettinger <python@rcn.com>
parents:
26172
diff
changeset
|
277 population contains repeats, then each occurrence is a possible |
|
83ae4a902df0
Improved clarity and thoroughness of docstring.
Raymond Hettinger <python@rcn.com>
parents:
26172
diff
changeset
|
278 selection in the sample. |
|
26155
8ceb455f5541
SF patch 629637: Add sample(population, k) method to the random module.
Raymond Hettinger <python@rcn.com>
parents:
24861
diff
changeset
|
279 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41592
diff
changeset
|
280 To choose a sample in a range of integers, use range as an argument. |
|
26178
83ae4a902df0
Improved clarity and thoroughness of docstring.
Raymond Hettinger <python@rcn.com>
parents:
26172
diff
changeset
|
281 This is especially fast and space efficient for sampling from a |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41592
diff
changeset
|
282 large population: sample(range(10000000), 60) |
|
26155
8ceb455f5541
SF patch 629637: Add sample(population, k) method to the random module.
Raymond Hettinger <python@rcn.com>
parents:
24861
diff
changeset
|
283 """ |
|
8ceb455f5541
SF patch 629637: Add sample(population, k) method to the random module.
Raymond Hettinger <python@rcn.com>
parents:
24861
diff
changeset
|
284 |
|
26178
83ae4a902df0
Improved clarity and thoroughness of docstring.
Raymond Hettinger <python@rcn.com>
parents:
26172
diff
changeset
|
285 # Sampling without replacement entails tracking either potential |
|
35359
c5bf5685267b
Implement random.sample() using sets instead of dicts.
Raymond Hettinger <python@rcn.com>
parents:
34942
diff
changeset
|
286 # selections (the pool) in a list or previous selections in a set. |
|
26178
83ae4a902df0
Improved clarity and thoroughness of docstring.
Raymond Hettinger <python@rcn.com>
parents:
26172
diff
changeset
|
287 |
| 31578 | 288 # When the number of selections is small compared to the |
| 289 # population, then tracking selections is efficient, requiring | |
|
35359
c5bf5685267b
Implement random.sample() using sets instead of dicts.
Raymond Hettinger <python@rcn.com>
parents:
34942
diff
changeset
|
290 # only a small set and an occasional reselection. For |
| 31578 | 291 # a larger number of selections, the pool tracking method is |
| 292 # preferred since the list takes less space than the | |
|
35359
c5bf5685267b
Implement random.sample() using sets instead of dicts.
Raymond Hettinger <python@rcn.com>
parents:
34942
diff
changeset
|
293 # set and it doesn't suffer from frequent reselections. |
|
26178
83ae4a902df0
Improved clarity and thoroughness of docstring.
Raymond Hettinger <python@rcn.com>
parents:
26172
diff
changeset
|
294 |
|
69859
e2f7db1255df
Sync-up minor code edits with the default branch.
Raymond Hettinger <python@rcn.com>
parents:
69857
diff
changeset
|
295 if isinstance(population, _Set): |
|
44665
91928d99ebb0
Take Tim's advice and have random.sample() support only sequences and sets.
Raymond Hettinger <python@rcn.com>
parents:
44662
diff
changeset
|
296 population = tuple(population) |
|
69859
e2f7db1255df
Sync-up minor code edits with the default branch.
Raymond Hettinger <python@rcn.com>
parents:
69857
diff
changeset
|
297 if not isinstance(population, _Sequence): |
|
e2f7db1255df
Sync-up minor code edits with the default branch.
Raymond Hettinger <python@rcn.com>
parents:
69857
diff
changeset
|
298 raise TypeError("Population must be a sequence or set. For dicts, use list(d).") |
|
64531
770c3ec05685
Minor refactoring and cleanup. Extend looping randrange() technique to subclasses.
Raymond Hettinger <python@rcn.com>
parents:
64523
diff
changeset
|
299 randbelow = self._randbelow |
|
26155
8ceb455f5541
SF patch 629637: Add sample(population, k) method to the random module.
Raymond Hettinger <python@rcn.com>
parents:
24861
diff
changeset
|
300 n = len(population) |
|
8ceb455f5541
SF patch 629637: Add sample(population, k) method to the random module.
Raymond Hettinger <python@rcn.com>
parents:
24861
diff
changeset
|
301 if not 0 <= k <= n: |
|
44665
91928d99ebb0
Take Tim's advice and have random.sample() support only sequences and sets.
Raymond Hettinger <python@rcn.com>
parents:
44662
diff
changeset
|
302 raise ValueError("Sample larger than population") |
|
26178
83ae4a902df0
Improved clarity and thoroughness of docstring.
Raymond Hettinger <python@rcn.com>
parents:
26172
diff
changeset
|
303 result = [None] * k |
|
35359
c5bf5685267b
Implement random.sample() using sets instead of dicts.
Raymond Hettinger <python@rcn.com>
parents:
34942
diff
changeset
|
304 setsize = 21 # size of a small set minus size of an empty list |
|
c5bf5685267b
Implement random.sample() using sets instead of dicts.
Raymond Hettinger <python@rcn.com>
parents:
34942
diff
changeset
|
305 if k > 5: |
|
35465
3e5c07faab11
Whitespace normalization (via reindent.py).
Tim Peters <tim.peters@gmail.com>
parents:
35359
diff
changeset
|
306 setsize += 4 ** _ceil(_log(k * 3, 4)) # table size for big sets |
|
44665
91928d99ebb0
Take Tim's advice and have random.sample() support only sequences and sets.
Raymond Hettinger <python@rcn.com>
parents:
44662
diff
changeset
|
307 if n <= setsize: |
|
91928d99ebb0
Take Tim's advice and have random.sample() support only sequences and sets.
Raymond Hettinger <python@rcn.com>
parents:
44662
diff
changeset
|
308 # An n-length list is smaller than a k-length set |
|
26266
42279f7d244d
Improve comments. Clarify docs.
Raymond Hettinger <python@rcn.com>
parents:
26178
diff
changeset
|
309 pool = list(population) |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41592
diff
changeset
|
310 for i in range(k): # invariant: non-selected at [0,n-i) |
|
64531
770c3ec05685
Minor refactoring and cleanup. Extend looping randrange() technique to subclasses.
Raymond Hettinger <python@rcn.com>
parents:
64523
diff
changeset
|
311 j = randbelow(n-i) |
|
26266
42279f7d244d
Improve comments. Clarify docs.
Raymond Hettinger <python@rcn.com>
parents:
26178
diff
changeset
|
312 result[i] = pool[j] |
|
27080
2ae1d17572da
Remove the random=None nonsense from sample() before it gets set in stone.
Raymond Hettinger <python@rcn.com>
parents:
26829
diff
changeset
|
313 pool[j] = pool[n-i-1] # move non-selected item into vacancy |
|
26178
83ae4a902df0
Improved clarity and thoroughness of docstring.
Raymond Hettinger <python@rcn.com>
parents:
26172
diff
changeset
|
314 else: |
|
44665
91928d99ebb0
Take Tim's advice and have random.sample() support only sequences and sets.
Raymond Hettinger <python@rcn.com>
parents:
44662
diff
changeset
|
315 selected = set() |
|
91928d99ebb0
Take Tim's advice and have random.sample() support only sequences and sets.
Raymond Hettinger <python@rcn.com>
parents:
44662
diff
changeset
|
316 selected_add = selected.add |
|
91928d99ebb0
Take Tim's advice and have random.sample() support only sequences and sets.
Raymond Hettinger <python@rcn.com>
parents:
44662
diff
changeset
|
317 for i in range(k): |
|
64531
770c3ec05685
Minor refactoring and cleanup. Extend looping randrange() technique to subclasses.
Raymond Hettinger <python@rcn.com>
parents:
64523
diff
changeset
|
318 j = randbelow(n) |
|
44665
91928d99ebb0
Take Tim's advice and have random.sample() support only sequences and sets.
Raymond Hettinger <python@rcn.com>
parents:
44662
diff
changeset
|
319 while j in selected: |
|
64531
770c3ec05685
Minor refactoring and cleanup. Extend looping randrange() technique to subclasses.
Raymond Hettinger <python@rcn.com>
parents:
64523
diff
changeset
|
320 j = randbelow(n) |
|
44665
91928d99ebb0
Take Tim's advice and have random.sample() support only sequences and sets.
Raymond Hettinger <python@rcn.com>
parents:
44662
diff
changeset
|
321 selected_add(j) |
|
91928d99ebb0
Take Tim's advice and have random.sample() support only sequences and sets.
Raymond Hettinger <python@rcn.com>
parents:
44662
diff
changeset
|
322 result[i] = population[j] |
|
26266
42279f7d244d
Improve comments. Clarify docs.
Raymond Hettinger <python@rcn.com>
parents:
26178
diff
changeset
|
323 return result |
|
26155
8ceb455f5541
SF patch 629637: Add sample(population, k) method to the random module.
Raymond Hettinger <python@rcn.com>
parents:
24861
diff
changeset
|
324 |
|
16265
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
325 ## -------------------- real-valued distributions ------------------- |
|
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
326 |
|
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
327 ## -------------------- 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
|
328 |
|
3c5f09422b69
Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents:
15871
diff
changeset
|
329 def uniform(self, a, b): |
|
54705
0e1b3c24d3e2
Issue 6261: Clarify behavior of random.uniform().
Raymond Hettinger <python@rcn.com>
parents:
51834
diff
changeset
|
330 "Get a random number in the range [a, b) or [a, b] depending on rounding." |
|
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 return a + (b-a) * self.random() |
| 1575 | 332 |
|
46034
0a485593ae1c
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44665
diff
changeset
|
333 ## -------------------- triangular -------------------- |
|
0a485593ae1c
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44665
diff
changeset
|
334 |
|
0a485593ae1c
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44665
diff
changeset
|
335 def triangular(self, low=0.0, high=1.0, mode=None): |
|
0a485593ae1c
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44665
diff
changeset
|
336 """Triangular distribution. |
|
0a485593ae1c
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44665
diff
changeset
|
337 |
|
0a485593ae1c
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44665
diff
changeset
|
338 Continuous distribution bounded by given lower and upper limits, |
|
0a485593ae1c
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44665
diff
changeset
|
339 and having a given mode value in-between. |
|
0a485593ae1c
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44665
diff
changeset
|
340 |
|
0a485593ae1c
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44665
diff
changeset
|
341 http://en.wikipedia.org/wiki/Triangular_distribution |
|
0a485593ae1c
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44665
diff
changeset
|
342 |
|
0a485593ae1c
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44665
diff
changeset
|
343 """ |
|
0a485593ae1c
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44665
diff
changeset
|
344 u = self.random() |
|
0a485593ae1c
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44665
diff
changeset
|
345 c = 0.5 if mode is None else (mode - low) / (high - low) |
|
0a485593ae1c
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44665
diff
changeset
|
346 if u > c: |
|
0a485593ae1c
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44665
diff
changeset
|
347 u = 1.0 - u |
|
0a485593ae1c
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44665
diff
changeset
|
348 c = 1.0 - c |
|
0a485593ae1c
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44665
diff
changeset
|
349 low, high = high, low |
|
0a485593ae1c
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44665
diff
changeset
|
350 return low + (high - low) * (u * c) ** 0.5 |
|
0a485593ae1c
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44665
diff
changeset
|
351 |
|
16265
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
352 ## -------------------- normal distribution -------------------- |
| 1575 | 353 |
|
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
|
354 def normalvariate(self, mu, sigma): |
|
23531
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
355 """Normal distribution. |
|
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
356 |
|
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
357 mu is the mean, and sigma is the standard deviation. |
|
23536
0a4c92559a0b
Whitespace normalized.
Raymond Hettinger <python@rcn.com>
parents:
23531
diff
changeset
|
358 |
|
23531
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
359 """ |
|
16242
3c5f09422b69
Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents:
15871
diff
changeset
|
360 # 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
|
361 |
|
3c5f09422b69
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 # 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
|
363 # 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
|
364 # 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
|
365 # Math Software, 3, (1977), pp257-260. |
| 1575 | 366 |
|
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
|
367 random = self.random |
|
34942
7287b41145ae
SF patch #1191489: Simplify logic in random.py
Raymond Hettinger <python@rcn.com>
parents:
34312
diff
changeset
|
368 while 1: |
|
16242
3c5f09422b69
Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents:
15871
diff
changeset
|
369 u1 = random() |
|
27085
90350b5d3397
Correct long standing bugs in the methods for random distributions.
Raymond Hettinger <python@rcn.com>
parents:
27080
diff
changeset
|
370 u2 = 1.0 - 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
|
371 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
|
372 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
|
373 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
|
374 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
|
375 return mu + z*sigma |
| 1575 | 376 |
|
16265
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
377 ## -------------------- lognormal distribution -------------------- |
| 1575 | 378 |
|
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 def lognormvariate(self, mu, sigma): |
|
23531
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
380 """Log normal distribution. |
|
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
381 |
|
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
382 If you take the natural logarithm of this distribution, you'll get a |
|
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
383 normal distribution with mean mu and standard deviation sigma. |
|
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
384 mu can have any value, and sigma must be greater than zero. |
|
23536
0a4c92559a0b
Whitespace normalized.
Raymond Hettinger <python@rcn.com>
parents:
23531
diff
changeset
|
385 |
|
23531
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
386 """ |
|
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
|
387 return _exp(self.normalvariate(mu, sigma)) |
| 1575 | 388 |
|
16265
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
389 ## -------------------- exponential distribution -------------------- |
| 1575 | 390 |
|
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
|
391 def expovariate(self, lambd): |
|
23531
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
392 """Exponential distribution. |
|
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
393 |
|
50626
fed2ae1e50c2
Merged revisions 68378 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
48596
diff
changeset
|
394 lambd is 1.0 divided by the desired mean. It should be |
|
fed2ae1e50c2
Merged revisions 68378 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
48596
diff
changeset
|
395 nonzero. (The parameter would be called "lambda", but that is |
|
fed2ae1e50c2
Merged revisions 68378 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
48596
diff
changeset
|
396 a reserved word in Python.) Returned values range from 0 to |
|
fed2ae1e50c2
Merged revisions 68378 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
48596
diff
changeset
|
397 positive infinity if lambd is positive, and from negative |
|
fed2ae1e50c2
Merged revisions 68378 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
48596
diff
changeset
|
398 infinity to 0 if lambd is negative. |
|
23536
0a4c92559a0b
Whitespace normalized.
Raymond Hettinger <python@rcn.com>
parents:
23531
diff
changeset
|
399 |
|
23531
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
400 """ |
|
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
|
401 # 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
|
402 # ('lambda' is a Python reserved word) |
| 1575 | 403 |
|
70949
9216e0a5d950
Code simplification suggested by Sven Marnach.
Raymond Hettinger <python@rcn.com>
parents:
69859
diff
changeset
|
404 # we use 1-random() instead of random() to preclude the |
|
9216e0a5d950
Code simplification suggested by Sven Marnach.
Raymond Hettinger <python@rcn.com>
parents:
69859
diff
changeset
|
405 # possibility of taking the log of zero. |
|
9216e0a5d950
Code simplification suggested by Sven Marnach.
Raymond Hettinger <python@rcn.com>
parents:
69859
diff
changeset
|
406 return -_log(1.0 - self.random())/lambd |
| 1575 | 407 |
|
16265
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
408 ## -------------------- von Mises distribution -------------------- |
| 1575 | 409 |
|
16242
3c5f09422b69
Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents:
15871
diff
changeset
|
410 def vonmisesvariate(self, mu, kappa): |
|
23531
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
411 """Circular data distribution. |
|
23536
0a4c92559a0b
Whitespace normalized.
Raymond Hettinger <python@rcn.com>
parents:
23531
diff
changeset
|
412 |
|
23531
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
413 mu is the mean angle, expressed in radians between 0 and 2*pi, and |
|
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
414 kappa is the concentration parameter, which must be greater than or |
|
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
415 equal to zero. If kappa is equal to zero, this distribution reduces |
|
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
416 to a uniform random angle over the range 0 to 2*pi. |
|
23536
0a4c92559a0b
Whitespace normalized.
Raymond Hettinger <python@rcn.com>
parents:
23531
diff
changeset
|
417 |
|
23531
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
418 """ |
|
16242
3c5f09422b69
Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents:
15871
diff
changeset
|
419 # 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
|
420 # 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
|
421 # if kappa = 0 generate uniform random angle |
| 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 # 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
|
424 # "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
|
425 # 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
|
426 |
|
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
|
427 # 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
|
428 # 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
|
429 |
|
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
|
430 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
|
431 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
|
432 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
|
433 |
|
16242
3c5f09422b69
Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents:
15871
diff
changeset
|
434 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
|
435 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
|
436 r = (1.0 + b * b)/(2.0 * b) |
| 1575 | 437 |
|
34942
7287b41145ae
SF patch #1191489: Simplify logic in random.py
Raymond Hettinger <python@rcn.com>
parents:
34312
diff
changeset
|
438 while 1: |
|
16242
3c5f09422b69
Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents:
15871
diff
changeset
|
439 u1 = random() |
| 1575 | 440 |
|
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
|
441 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
|
442 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
|
443 c = kappa * (r - f) |
| 1575 | 444 |
|
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
|
445 u2 = random() |
| 1575 | 446 |
|
34942
7287b41145ae
SF patch #1191489: Simplify logic in random.py
Raymond Hettinger <python@rcn.com>
parents:
34312
diff
changeset
|
447 if u2 < c * (2.0 - c) or u2 <= c * _exp(1.0 - c): |
|
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
|
448 break |
| 1575 | 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 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
|
451 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
|
452 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
|
453 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
|
454 theta = (mu % TWOPI) - _acos(f) |
| 1575 | 455 |
|
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
|
456 return theta |
| 1575 | 457 |
|
16265
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
458 ## -------------------- gamma distribution -------------------- |
| 1575 | 459 |
|
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
|
460 def gammavariate(self, alpha, beta): |
|
23531
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
461 """Gamma distribution. Not the gamma function! |
|
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
462 |
|
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
463 Conditions on the parameters are alpha > 0 and beta > 0. |
|
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
464 |
|
68846
e880020e7e6a
Issue 10787: Document the probability density function for random.gammavariate.
Raymond Hettinger <python@rcn.com>
parents:
57584
diff
changeset
|
465 The probability distribution function is: |
|
e880020e7e6a
Issue 10787: Document the probability density function for random.gammavariate.
Raymond Hettinger <python@rcn.com>
parents:
57584
diff
changeset
|
466 |
|
e880020e7e6a
Issue 10787: Document the probability density function for random.gammavariate.
Raymond Hettinger <python@rcn.com>
parents:
57584
diff
changeset
|
467 x ** (alpha - 1) * math.exp(-x / beta) |
|
e880020e7e6a
Issue 10787: Document the probability density function for random.gammavariate.
Raymond Hettinger <python@rcn.com>
parents:
57584
diff
changeset
|
468 pdf(x) = -------------------------------------- |
|
e880020e7e6a
Issue 10787: Document the probability density function for random.gammavariate.
Raymond Hettinger <python@rcn.com>
parents:
57584
diff
changeset
|
469 math.gamma(alpha) * beta ** alpha |
|
e880020e7e6a
Issue 10787: Document the probability density function for random.gammavariate.
Raymond Hettinger <python@rcn.com>
parents:
57584
diff
changeset
|
470 |
|
23531
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
471 """ |
|
23521
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23451
diff
changeset
|
472 |
|
23447
08ccb0f3e2d4
Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents:
23442
diff
changeset
|
473 # alpha > 0, beta > 0, mean is alpha*beta, variance is alpha*beta**2 |
|
23521
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23451
diff
changeset
|
474 |
|
23451
273dd2508c57
Use spaces, not tabs for indents.
Guido van Rossum <guido@python.org>
parents:
23447
diff
changeset
|
475 # Warning: a few older sources define the gamma distribution in terms |
|
273dd2508c57
Use spaces, not tabs for indents.
Guido van Rossum <guido@python.org>
parents:
23447
diff
changeset
|
476 # of alpha > -1.0 |
|
273dd2508c57
Use spaces, not tabs for indents.
Guido van Rossum <guido@python.org>
parents:
23447
diff
changeset
|
477 if alpha <= 0.0 or beta <= 0.0: |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42084
diff
changeset
|
478 raise ValueError('gammavariate: alpha and beta must be > 0.0') |
|
23521
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23451
diff
changeset
|
479 |
|
16242
3c5f09422b69
Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents:
15871
diff
changeset
|
480 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
|
481 if alpha > 1.0: |
| 1575 | 482 |
|
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
|
483 # 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
|
484 # 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
|
485 # Applied Statistics, (1977), 26, No. 1, p71-74 |
| 1575 | 486 |
|
23442
b15896053933
Closes SF bug 527139. Moved temp variables inside guard.
Raymond Hettinger <python@rcn.com>
parents:
23380
diff
changeset
|
487 ainv = _sqrt(2.0 * alpha - 1.0) |
|
b15896053933
Closes SF bug 527139. Moved temp variables inside guard.
Raymond Hettinger <python@rcn.com>
parents:
23380
diff
changeset
|
488 bbb = alpha - LOG4 |
|
b15896053933
Closes SF bug 527139. Moved temp variables inside guard.
Raymond Hettinger <python@rcn.com>
parents:
23380
diff
changeset
|
489 ccc = alpha + ainv |
|
23521
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23451
diff
changeset
|
490 |
|
34942
7287b41145ae
SF patch #1191489: Simplify logic in random.py
Raymond Hettinger <python@rcn.com>
parents:
34312
diff
changeset
|
491 while 1: |
|
16242
3c5f09422b69
Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents:
15871
diff
changeset
|
492 u1 = random() |
|
27085
90350b5d3397
Correct long standing bugs in the methods for random distributions.
Raymond Hettinger <python@rcn.com>
parents:
27080
diff
changeset
|
493 if not 1e-7 < u1 < .9999999: |
|
90350b5d3397
Correct long standing bugs in the methods for random distributions.
Raymond Hettinger <python@rcn.com>
parents:
27080
diff
changeset
|
494 continue |
|
90350b5d3397
Correct long standing bugs in the methods for random distributions.
Raymond Hettinger <python@rcn.com>
parents:
27080
diff
changeset
|
495 u2 = 1.0 - 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
|
496 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
|
497 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
|
498 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
|
499 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
|
500 if r + SG_MAGICCONST - 4.5*z >= 0.0 or r >= _log(z): |
|
23447
08ccb0f3e2d4
Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents:
23442
diff
changeset
|
501 return x * beta |
| 1575 | 502 |
|
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
|
503 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
|
504 # expovariate(1) |
|
15871
62aeefa01fdd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14617
diff
changeset
|
505 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
|
506 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
|
507 u = random() |
|
23447
08ccb0f3e2d4
Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents:
23442
diff
changeset
|
508 return -_log(u) * beta |
| 1575 | 509 |
|
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
|
510 else: # alpha is between 0 and 1 (exclusive) |
| 1575 | 511 |
|
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
|
512 # Uses ALGORITHM GS of Statistical Computing - Kennedy & Gentle |
| 1575 | 513 |
|
34942
7287b41145ae
SF patch #1191489: Simplify logic in random.py
Raymond Hettinger <python@rcn.com>
parents:
34312
diff
changeset
|
514 while 1: |
|
16242
3c5f09422b69
Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents:
15871
diff
changeset
|
515 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
|
516 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
|
517 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
|
518 if p <= 1.0: |
|
34942
7287b41145ae
SF patch #1191489: Simplify logic in random.py
Raymond Hettinger <python@rcn.com>
parents:
34312
diff
changeset
|
519 x = p ** (1.0/alpha) |
|
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
|
520 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
|
521 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
|
522 u1 = random() |
|
34942
7287b41145ae
SF patch #1191489: Simplify logic in random.py
Raymond Hettinger <python@rcn.com>
parents:
34312
diff
changeset
|
523 if p > 1.0: |
|
7287b41145ae
SF patch #1191489: Simplify logic in random.py
Raymond Hettinger <python@rcn.com>
parents:
34312
diff
changeset
|
524 if u1 <= x ** (alpha - 1.0): |
|
7287b41145ae
SF patch #1191489: Simplify logic in random.py
Raymond Hettinger <python@rcn.com>
parents:
34312
diff
changeset
|
525 break |
|
7287b41145ae
SF patch #1191489: Simplify logic in random.py
Raymond Hettinger <python@rcn.com>
parents:
34312
diff
changeset
|
526 elif u1 <= _exp(-x): |
|
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
|
527 break |
|
23447
08ccb0f3e2d4
Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents:
23442
diff
changeset
|
528 return x * beta |
|
08ccb0f3e2d4
Closes patch 529408 deprecating random.stdgamma().
Raymond Hettinger <python@rcn.com>
parents:
23442
diff
changeset
|
529 |
|
16265
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
530 ## -------------------- 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
|
531 |
|
16242
3c5f09422b69
Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents:
15871
diff
changeset
|
532 def gauss(self, mu, sigma): |
|
23531
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
533 """Gaussian distribution. |
|
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
534 |
|
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
535 mu is the mean, and sigma is the standard deviation. This is |
|
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
536 slightly faster than the normalvariate() function. |
|
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
537 |
|
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
538 Not thread-safe without a lock around calls. |
|
23536
0a4c92559a0b
Whitespace normalized.
Raymond Hettinger <python@rcn.com>
parents:
23531
diff
changeset
|
539 |
|
23531
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
540 """ |
|
1577
444e349f962f
Use float constants directly; cosmetic changes; initialize largest
Guido van Rossum <guido@python.org>
parents:
1576
diff
changeset
|
541 |
|
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
|
542 # 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
|
543 # 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
|
544 # |
|
3c5f09422b69
Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents:
15871
diff
changeset
|
545 # 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
|
546 # 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
|
547 # |
|
3c5f09422b69
Reworked random.py so that it no longer depends on, and offers all the
Tim Peters <tim.peters@gmail.com>
parents:
15871
diff
changeset
|
548 # 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
|
549 # (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
|
550 # (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
|
551 # (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
|
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 # 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
|
554 # 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
|
555 # 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
|
556 # 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
|
557 # 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
|
558 # lock here.) |
|
1577
444e349f962f
Use float constants directly; cosmetic changes; initialize largest
Guido van Rossum <guido@python.org>
parents:
1576
diff
changeset
|
559 |
|
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
|
560 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
|
561 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
|
562 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
|
563 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
|
564 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
|
565 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
|
566 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
|
567 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
|
568 |
|
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
|
569 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
|
570 |
|
16265
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
571 ## -------------------- beta -------------------- |
|
16271
7ef2382f6123
SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents:
16265
diff
changeset
|
572 ## See |
|
69362
f5b9ad73157c
#11848: replace dead link in random.betavariate comment.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
68846
diff
changeset
|
573 ## http://mail.python.org/pipermail/python-bugs-list/2001-January/003752.html |
|
16271
7ef2382f6123
SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents:
16265
diff
changeset
|
574 ## 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
|
575 ## |
|
7ef2382f6123
SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents:
16265
diff
changeset
|
576 ## def betavariate(self, alpha, beta): |
|
7ef2382f6123
SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents:
16265
diff
changeset
|
577 ## # 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
|
578 ## |
|
7ef2382f6123
SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents:
16265
diff
changeset
|
579 ## y = self.expovariate(alpha) |
|
7ef2382f6123
SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents:
16265
diff
changeset
|
580 ## 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
|
581 ## return z/(y+z) |
|
7ef2382f6123
SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents:
16265
diff
changeset
|
582 ## |
|
7ef2382f6123
SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents:
16265
diff
changeset
|
583 ## 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
|
584 |
|
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
|
585 def betavariate(self, alpha, beta): |
|
23531
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
586 """Beta distribution. |
|
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
587 |
|
40737
6ebad2a6b990
Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40644
diff
changeset
|
588 Conditions on the parameters are alpha > 0 and beta > 0. |
|
23531
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
589 Returned values range between 0 and 1. |
|
23536
0a4c92559a0b
Whitespace normalized.
Raymond Hettinger <python@rcn.com>
parents:
23531
diff
changeset
|
590 |
|
23531
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
591 """ |
|
23536
0a4c92559a0b
Whitespace normalized.
Raymond Hettinger <python@rcn.com>
parents:
23531
diff
changeset
|
592 |
|
16271
7ef2382f6123
SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents:
16265
diff
changeset
|
593 # 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
|
594 # 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
|
595 y = self.gammavariate(alpha, 1.) |
|
7ef2382f6123
SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents:
16265
diff
changeset
|
596 if y == 0: |
|
7ef2382f6123
SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents:
16265
diff
changeset
|
597 return 0.0 |
|
7ef2382f6123
SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents:
16265
diff
changeset
|
598 else: |
|
7ef2382f6123
SF bug 130030: Claim of bad betavariate algorithm.
Tim Peters <tim.peters@gmail.com>
parents:
16265
diff
changeset
|
599 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
|
600 |
|
16265
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
601 ## -------------------- Pareto -------------------- |
|
6698
b73eb4e6face
Added Pareto and Weibull distributions, courtesy Jeremy Hylton.
Guido van Rossum <guido@python.org>
parents:
1618
diff
changeset
|
602 |
|
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
|
603 def paretovariate(self, alpha): |
|
23531
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
604 """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
|
605 # Jain, pg. 495 |
|
6698
b73eb4e6face
Added Pareto and Weibull distributions, courtesy Jeremy Hylton.
Guido van Rossum <guido@python.org>
parents:
1618
diff
changeset
|
606 |
|
27085
90350b5d3397
Correct long standing bugs in the methods for random distributions.
Raymond Hettinger <python@rcn.com>
parents:
27080
diff
changeset
|
607 u = 1.0 - self.random() |
|
64568
7c2eb5b6cc58
One more conversion from pow() to **.
Raymond Hettinger <python@rcn.com>
parents:
64567
diff
changeset
|
608 return 1.0 / u ** (1.0/alpha) |
|
6698
b73eb4e6face
Added Pareto and Weibull distributions, courtesy Jeremy Hylton.
Guido van Rossum <guido@python.org>
parents:
1618
diff
changeset
|
609 |
|
16265
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
610 ## -------------------- Weibull -------------------- |
|
6698
b73eb4e6face
Added Pareto and Weibull distributions, courtesy Jeremy Hylton.
Guido van Rossum <guido@python.org>
parents:
1618
diff
changeset
|
611 |
|
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
|
612 def weibullvariate(self, alpha, beta): |
|
23531
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
613 """Weibull distribution. |
|
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
614 |
|
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
615 alpha is the scale parameter and beta is the shape parameter. |
|
23536
0a4c92559a0b
Whitespace normalized.
Raymond Hettinger <python@rcn.com>
parents:
23531
diff
changeset
|
616 |
|
23531
a98e93eafef7
Deprecated Random.cunifvariate clearing bug 506647. Also, added docstrings.
Raymond Hettinger <python@rcn.com>
parents:
23521
diff
changeset
|
617 """ |
|
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
|
618 # 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
|
619 |
|
27085
90350b5d3397
Correct long standing bugs in the methods for random distributions.
Raymond Hettinger <python@rcn.com>
parents:
27080
diff
changeset
|
620 u = 1.0 - self.random() |
|
64567
b1865ca7b65c
* Remove dependency on binascii.hexlify by using int.from_bytes().
Raymond Hettinger <python@rcn.com>
parents:
64553
diff
changeset
|
621 return alpha * (-_log(u)) ** (1.0/beta) |
|
11200
bca2d14f3e36
Add Tim Peters' shuffle() algorithm.
Guido van Rossum <guido@python.org>
parents:
8802
diff
changeset
|
622 |
|
33481
eacdc29606d8
SF #1027105: HardwareRandom should be renamed OSRandom
Raymond Hettinger <python@rcn.com>
parents:
33409
diff
changeset
|
623 ## --------------- Operating System Random Source ------------------ |
|
33329
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32287
diff
changeset
|
624 |
|
33481
eacdc29606d8
SF #1027105: HardwareRandom should be renamed OSRandom
Raymond Hettinger <python@rcn.com>
parents:
33409
diff
changeset
|
625 class SystemRandom(Random): |
|
eacdc29606d8
SF #1027105: HardwareRandom should be renamed OSRandom
Raymond Hettinger <python@rcn.com>
parents:
33409
diff
changeset
|
626 """Alternate random number generator using sources provided |
|
eacdc29606d8
SF #1027105: HardwareRandom should be renamed OSRandom
Raymond Hettinger <python@rcn.com>
parents:
33409
diff
changeset
|
627 by the operating system (such as /dev/urandom on Unix or |
|
eacdc29606d8
SF #1027105: HardwareRandom should be renamed OSRandom
Raymond Hettinger <python@rcn.com>
parents:
33409
diff
changeset
|
628 CryptGenRandom on Windows). |
|
33329
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32287
diff
changeset
|
629 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32287
diff
changeset
|
630 Not available on all systems (see os.urandom() for details). |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32287
diff
changeset
|
631 """ |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32287
diff
changeset
|
632 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32287
diff
changeset
|
633 def random(self): |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32287
diff
changeset
|
634 """Get the next random number in the range [0.0, 1.0).""" |
|
64567
b1865ca7b65c
* Remove dependency on binascii.hexlify by using int.from_bytes().
Raymond Hettinger <python@rcn.com>
parents:
64553
diff
changeset
|
635 return (int.from_bytes(_urandom(7), 'big') >> 3) * RECIP_BPF |
|
33329
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32287
diff
changeset
|
636 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32287
diff
changeset
|
637 def getrandbits(self, k): |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32287
diff
changeset
|
638 """getrandbits(k) -> x. Generates a long int with k random bits.""" |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32287
diff
changeset
|
639 if k <= 0: |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32287
diff
changeset
|
640 raise ValueError('number of bits must be greater than zero') |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32287
diff
changeset
|
641 if k != int(k): |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32287
diff
changeset
|
642 raise TypeError('number of bits should be an integer') |
|
64569
8627c3ec9711
Improve variable name (don't shadow a builtin).
Raymond Hettinger <python@rcn.com>
parents:
64568
diff
changeset
|
643 numbytes = (k + 7) // 8 # bits / 8 and rounded up |
|
8627c3ec9711
Improve variable name (don't shadow a builtin).
Raymond Hettinger <python@rcn.com>
parents:
64568
diff
changeset
|
644 x = int.from_bytes(_urandom(numbytes), 'big') |
|
8627c3ec9711
Improve variable name (don't shadow a builtin).
Raymond Hettinger <python@rcn.com>
parents:
64568
diff
changeset
|
645 return x >> (numbytes * 8 - k) # trim excess bits |
|
33329
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32287
diff
changeset
|
646 |
|
44662
c25760c21397
Remove defunct parts of the random module
Raymond Hettinger <python@rcn.com>
parents:
44129
diff
changeset
|
647 def seed(self, *args, **kwds): |
|
33481
eacdc29606d8
SF #1027105: HardwareRandom should be renamed OSRandom
Raymond Hettinger <python@rcn.com>
parents:
33409
diff
changeset
|
648 "Stub method. Not used for a system random number generator." |
|
33329
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32287
diff
changeset
|
649 return None |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32287
diff
changeset
|
650 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32287
diff
changeset
|
651 def _notimplemented(self, *args, **kwds): |
|
33481
eacdc29606d8
SF #1027105: HardwareRandom should be renamed OSRandom
Raymond Hettinger <python@rcn.com>
parents:
33409
diff
changeset
|
652 "Method should not be called for a system random number generator." |
|
eacdc29606d8
SF #1027105: HardwareRandom should be renamed OSRandom
Raymond Hettinger <python@rcn.com>
parents:
33409
diff
changeset
|
653 raise NotImplementedError('System entropy source does not have state.') |
|
33329
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32287
diff
changeset
|
654 getstate = setstate = _notimplemented |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32287
diff
changeset
|
655 |
|
16265
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
656 ## -------------------- test program -------------------- |
| 1575 | 657 |
|
30243
53a322c6de52
Eliminate unnecessary eval() in tests
Raymond Hettinger <python@rcn.com>
parents:
30114
diff
changeset
|
658 def _test_generator(n, func, args): |
|
15871
62aeefa01fdd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14617
diff
changeset
|
659 import time |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
660 print(n, 'times', func.__name__) |
|
29250
34609fa35b11
'sum' is no longer a good variable name. Use 'total' instead.
Raymond Hettinger <python@rcn.com>
parents:
27279
diff
changeset
|
661 total = 0.0 |
|
15871
62aeefa01fdd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14617
diff
changeset
|
662 sqsum = 0.0 |
|
62aeefa01fdd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14617
diff
changeset
|
663 smallest = 1e10 |
|
62aeefa01fdd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14617
diff
changeset
|
664 largest = -1e10 |
|
62aeefa01fdd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14617
diff
changeset
|
665 t0 = time.time() |
|
62aeefa01fdd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14617
diff
changeset
|
666 for i in range(n): |
|
30243
53a322c6de52
Eliminate unnecessary eval() in tests
Raymond Hettinger <python@rcn.com>
parents:
30114
diff
changeset
|
667 x = func(*args) |
|
29250
34609fa35b11
'sum' is no longer a good variable name. Use 'total' instead.
Raymond Hettinger <python@rcn.com>
parents:
27279
diff
changeset
|
668 total += x |
|
15871
62aeefa01fdd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14617
diff
changeset
|
669 sqsum = sqsum + x*x |
|
62aeefa01fdd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14617
diff
changeset
|
670 smallest = min(x, smallest) |
|
62aeefa01fdd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14617
diff
changeset
|
671 largest = max(x, largest) |
|
62aeefa01fdd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14617
diff
changeset
|
672 t1 = time.time() |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
673 print(round(t1-t0, 3), 'sec,', end=' ') |
|
29250
34609fa35b11
'sum' is no longer a good variable name. Use 'total' instead.
Raymond Hettinger <python@rcn.com>
parents:
27279
diff
changeset
|
674 avg = total/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
|
675 stddev = _sqrt(sqsum/n - avg*avg) |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
676 print('avg %g, stddev %g, min %g, max %g' % \ |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
677 (avg, stddev, smallest, largest)) |
| 1575 | 678 |
|
26155
8ceb455f5541
SF patch 629637: Add sample(population, k) method to the random module.
Raymond Hettinger <python@rcn.com>
parents:
24861
diff
changeset
|
679 |
|
8ceb455f5541
SF patch 629637: Add sample(population, k) method to the random module.
Raymond Hettinger <python@rcn.com>
parents:
24861
diff
changeset
|
680 def _test(N=2000): |
|
30243
53a322c6de52
Eliminate unnecessary eval() in tests
Raymond Hettinger <python@rcn.com>
parents:
30114
diff
changeset
|
681 _test_generator(N, random, ()) |
|
53a322c6de52
Eliminate unnecessary eval() in tests
Raymond Hettinger <python@rcn.com>
parents:
30114
diff
changeset
|
682 _test_generator(N, normalvariate, (0.0, 1.0)) |
|
53a322c6de52
Eliminate unnecessary eval() in tests
Raymond Hettinger <python@rcn.com>
parents:
30114
diff
changeset
|
683 _test_generator(N, lognormvariate, (0.0, 1.0)) |
|
53a322c6de52
Eliminate unnecessary eval() in tests
Raymond Hettinger <python@rcn.com>
parents:
30114
diff
changeset
|
684 _test_generator(N, vonmisesvariate, (0.0, 1.0)) |
|
53a322c6de52
Eliminate unnecessary eval() in tests
Raymond Hettinger <python@rcn.com>
parents:
30114
diff
changeset
|
685 _test_generator(N, gammavariate, (0.01, 1.0)) |
|
53a322c6de52
Eliminate unnecessary eval() in tests
Raymond Hettinger <python@rcn.com>
parents:
30114
diff
changeset
|
686 _test_generator(N, gammavariate, (0.1, 1.0)) |
|
53a322c6de52
Eliminate unnecessary eval() in tests
Raymond Hettinger <python@rcn.com>
parents:
30114
diff
changeset
|
687 _test_generator(N, gammavariate, (0.1, 2.0)) |
|
53a322c6de52
Eliminate unnecessary eval() in tests
Raymond Hettinger <python@rcn.com>
parents:
30114
diff
changeset
|
688 _test_generator(N, gammavariate, (0.5, 1.0)) |
|
53a322c6de52
Eliminate unnecessary eval() in tests
Raymond Hettinger <python@rcn.com>
parents:
30114
diff
changeset
|
689 _test_generator(N, gammavariate, (0.9, 1.0)) |
|
53a322c6de52
Eliminate unnecessary eval() in tests
Raymond Hettinger <python@rcn.com>
parents:
30114
diff
changeset
|
690 _test_generator(N, gammavariate, (1.0, 1.0)) |
|
53a322c6de52
Eliminate unnecessary eval() in tests
Raymond Hettinger <python@rcn.com>
parents:
30114
diff
changeset
|
691 _test_generator(N, gammavariate, (2.0, 1.0)) |
|
53a322c6de52
Eliminate unnecessary eval() in tests
Raymond Hettinger <python@rcn.com>
parents:
30114
diff
changeset
|
692 _test_generator(N, gammavariate, (20.0, 1.0)) |
|
53a322c6de52
Eliminate unnecessary eval() in tests
Raymond Hettinger <python@rcn.com>
parents:
30114
diff
changeset
|
693 _test_generator(N, gammavariate, (200.0, 1.0)) |
|
53a322c6de52
Eliminate unnecessary eval() in tests
Raymond Hettinger <python@rcn.com>
parents:
30114
diff
changeset
|
694 _test_generator(N, gauss, (0.0, 1.0)) |
|
53a322c6de52
Eliminate unnecessary eval() in tests
Raymond Hettinger <python@rcn.com>
parents:
30114
diff
changeset
|
695 _test_generator(N, betavariate, (3.0, 3.0)) |
|
46034
0a485593ae1c
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44665
diff
changeset
|
696 _test_generator(N, triangular, (0.0, 1.0, 1.0/3.0)) |
|
16265
d614b6e9fb4e
Cosmetic changes after some sleep; no change in semantics.
Tim Peters <tim.peters@gmail.com>
parents:
16243
diff
changeset
|
697 |
|
16292
0501c2a10315
New comment block to Clarify a subtlety.
Tim Peters <tim.peters@gmail.com>
parents:
16272
diff
changeset
|
698 # Create one instance, seeded from current time, and export its methods |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
26564
diff
changeset
|
699 # as module-level functions. The functions share state across all uses |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
26564
diff
changeset
|
700 #(both in the user's code and in the Python libraries), but that's fine |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
26564
diff
changeset
|
701 # for most programs and is easier for the casual user than making them |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
26564
diff
changeset
|
702 # instantiate their own Random() instance. |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
26564
diff
changeset
|
703 |
|
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
|
704 _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
|
705 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
|
706 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
|
707 uniform = _inst.uniform |
|
46034
0a485593ae1c
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44665
diff
changeset
|
708 triangular = _inst.triangular |
|
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
|
709 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
|
710 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
|
711 randrange = _inst.randrange |
|
26155
8ceb455f5541
SF patch 629637: Add sample(population, k) method to the random module.
Raymond Hettinger <python@rcn.com>
parents:
24861
diff
changeset
|
712 sample = _inst.sample |
|
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
|
713 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
|
714 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
|
715 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
|
716 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
|
717 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
|
718 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
|
719 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
|
720 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
|
721 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
|
722 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
|
723 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
|
724 setstate = _inst.setstate |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
725 getrandbits = _inst.getrandbits |
|
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
|
726 |
| 1575 | 727 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
|
728 _test() |
