Mercurial > cpython-lru_cache-weakref
annotate Modules/_randommodule.c @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | 6df0b4ed8617 |
| children |
| rev | line source |
|---|---|
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
1 /* Random objects */ |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
2 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
3 /* ------------------------------------------------------------------ |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
4 The code in this module was based on a download from: |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
5 http://www.math.keio.ac.jp/~matumoto/MT2002/emt19937ar.html |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
6 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
7 It was modified in 2002 by Raymond Hettinger as follows: |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
8 |
|
64264
f91f29ef4ee7
Merged revisions 84299 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
61463
diff
changeset
|
9 * the principal computational lines untouched. |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
10 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
11 * renamed genrand_res53() to random_random() and wrapped |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
12 in python calling/return code. |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
13 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
14 * genrand_int32() and the helper functions, init_genrand() |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
15 and init_by_array(), were declared static, wrapped in |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
16 Python calling/return code. also, their global data |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
17 references were replaced with structure references. |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
18 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
19 * unused functions from the original were deleted. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
20 new, original C python code was added to implement the |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
21 Random() interface. |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
22 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
23 The following are the verbatim comments from the original code: |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
24 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
25 A C-program for MT19937, with initialization improved 2002/1/26. |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
26 Coded by Takuji Nishimura and Makoto Matsumoto. |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
27 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
28 Before using, initialize the state by using init_genrand(seed) |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
29 or init_by_array(init_key, key_length). |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
30 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
31 Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
32 All rights reserved. |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
33 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
34 Redistribution and use in source and binary forms, with or without |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
35 modification, are permitted provided that the following conditions |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
36 are met: |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
37 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
38 1. Redistributions of source code must retain the above copyright |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
39 notice, this list of conditions and the following disclaimer. |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
40 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
41 2. Redistributions in binary form must reproduce the above copyright |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
42 notice, this list of conditions and the following disclaimer in the |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
43 documentation and/or other materials provided with the distribution. |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
44 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
45 3. The names of its contributors may not be used to endorse or promote |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
46 products derived from this software without specific prior written |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
47 permission. |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
48 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
49 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
50 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
51 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
52 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
53 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
54 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
55 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
56 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
57 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
58 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
59 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
60 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
61 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
62 Any feedback is very welcome. |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
63 http://www.math.keio.ac.jp/matumoto/emt.html |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
64 email: matumoto@math.keio.ac.jp |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
65 */ |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
66 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
67 /* ---------------------------------------------------------------*/ |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
68 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
69 #include "Python.h" |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
70 #include <time.h> /* for seeding to current time */ |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
71 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
72 /* Period parameters -- These are all magic. Don't change. */ |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
73 #define N 624 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
74 #define M 397 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
75 #define MATRIX_A 0x9908b0dfUL /* constant vector a */ |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
76 #define UPPER_MASK 0x80000000UL /* most significant w-r bits */ |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
77 #define LOWER_MASK 0x7fffffffUL /* least significant r bits */ |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
78 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
79 typedef struct { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
80 PyObject_HEAD |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
81 unsigned long state[N]; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
82 int index; |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
83 } RandomObject; |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
84 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
85 static PyTypeObject Random_Type; |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
86 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
87 #define RandomObject_Check(v) (Py_TYPE(v) == &Random_Type) |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
88 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
89 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
90 /* Random methods */ |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
91 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
92 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
93 /* generates a random number on [0,0xffffffff]-interval */ |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
94 static unsigned long |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
95 genrand_int32(RandomObject *self) |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
96 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
97 unsigned long y; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
98 static unsigned long mag01[2]={0x0UL, MATRIX_A}; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
99 /* mag01[x] = x * MATRIX_A for x=0,1 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
100 unsigned long *mt; |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
101 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
102 mt = self->state; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
103 if (self->index >= N) { /* generate N words at one time */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
104 int kk; |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
105 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
106 for (kk=0;kk<N-M;kk++) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
107 y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
108 mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL]; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
109 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
110 for (;kk<N-1;kk++) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
111 y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
112 mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL]; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
113 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
114 y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
115 mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL]; |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
116 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
117 self->index = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
118 } |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
119 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
120 y = mt[self->index++]; |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
121 y ^= (y >> 11); |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
122 y ^= (y << 7) & 0x9d2c5680UL; |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
123 y ^= (y << 15) & 0xefc60000UL; |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
124 y ^= (y >> 18); |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
125 return y; |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
126 } |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
127 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
128 /* random_random is the function named genrand_res53 in the original code; |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
129 * generates a random number on [0,1) with 53-bit resolution; note that |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
130 * 9007199254740992 == 2**53; I assume they're spelling "/2**53" as |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
131 * multiply-by-reciprocal in the (likely vain) hope that the compiler will |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
132 * optimize the division away at compile-time. 67108864 is 2**26. In |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
133 * effect, a contains 27 random bits shifted left 26, and b fills in the |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
134 * lower 26 bits of the 53-bit numerator. |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
135 * The orginal code credited Isaku Wada for this algorithm, 2002/01/09. |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
136 */ |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
137 static PyObject * |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
138 random_random(RandomObject *self) |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
139 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
140 unsigned long a=genrand_int32(self)>>5, b=genrand_int32(self)>>6; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
141 return PyFloat_FromDouble((a*67108864.0+b)*(1.0/9007199254740992.0)); |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
142 } |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
143 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
144 /* initializes mt[N] with a seed */ |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
145 static void |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
146 init_genrand(RandomObject *self, unsigned long s) |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
147 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
148 int mti; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
149 unsigned long *mt; |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
150 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
151 mt = self->state; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
152 mt[0]= s & 0xffffffffUL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
153 for (mti=1; mti<N; mti++) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
154 mt[mti] = |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
155 (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
156 /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
157 /* In the previous versions, MSBs of the seed affect */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
158 /* only MSBs of the array mt[]. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
159 /* 2002/01/09 modified by Makoto Matsumoto */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
160 mt[mti] &= 0xffffffffUL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
161 /* for >32 bit machines */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
162 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
163 self->index = mti; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
164 return; |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
165 } |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
166 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
167 /* initialize by an array with array-length */ |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
168 /* init_key is the array for initializing keys */ |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
169 /* key_length is its length */ |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
170 static PyObject * |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
171 init_by_array(RandomObject *self, unsigned long init_key[], unsigned long key_length) |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
172 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
173 unsigned int i, j, k; /* was signed in the original code. RDH 12/16/2002 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
174 unsigned long *mt; |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
175 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
176 mt = self->state; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
177 init_genrand(self, 19650218UL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
178 i=1; j=0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
179 k = (N>key_length ? N : key_length); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
180 for (; k; k--) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
181 mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
182 + init_key[j] + j; /* non linear */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
183 mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
184 i++; j++; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
185 if (i>=N) { mt[0] = mt[N-1]; i=1; } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
186 if (j>=key_length) j=0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
187 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
188 for (k=N-1; k; k--) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
189 mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
190 - i; /* non linear */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
191 mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
192 i++; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
193 if (i>=N) { mt[0] = mt[N-1]; i=1; } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
194 } |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
195 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
196 mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */ |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
197 Py_INCREF(Py_None); |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
198 return Py_None; |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
199 } |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
200 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
201 /* |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
202 * The rest is Python-specific code, neither part of, nor derived from, the |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
203 * Twister download. |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
204 */ |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
205 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
206 static PyObject * |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
207 random_seed(RandomObject *self, PyObject *args) |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
208 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
209 PyObject *result = NULL; /* guilty until proved innocent */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
210 PyObject *masklower = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
211 PyObject *thirtytwo = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
212 PyObject *n = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
213 unsigned long *key = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
214 unsigned long keymax; /* # of allocated slots in key */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
215 unsigned long keyused; /* # of used slots in key */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
216 int err; |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
217 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
218 PyObject *arg = NULL; |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
219 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
220 if (!PyArg_UnpackTuple(args, "seed", 0, 1, &arg)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
221 return NULL; |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
222 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
223 if (arg == NULL || arg == Py_None) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
224 time_t now; |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
225 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
226 time(&now); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
227 init_genrand(self, (unsigned long)now); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
228 Py_INCREF(Py_None); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
229 return Py_None; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
230 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
231 /* If the arg is an int or long, use its absolute value; else use |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
232 * the absolute value of its hash code. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
233 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
234 if (PyInt_Check(arg) || PyLong_Check(arg)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
235 n = PyNumber_Absolute(arg); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
236 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
237 long hash = PyObject_Hash(arg); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
238 if (hash == -1) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
239 goto Done; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
240 n = PyLong_FromUnsignedLong((unsigned long)hash); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
241 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
242 if (n == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
243 goto Done; |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
244 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
245 /* Now split n into 32-bit chunks, from the right. Each piece is |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
246 * stored into key, which has a capacity of keymax chunks, of which |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
247 * keyused are filled. Alas, the repeated shifting makes this a |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
248 * quadratic-time algorithm; we'd really like to use |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
249 * _PyLong_AsByteArray here, but then we'd have to break into the |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
250 * long representation to figure out how big an array was needed |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
251 * in advance. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
252 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
253 keymax = 8; /* arbitrary; grows later if needed */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
254 keyused = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
255 key = (unsigned long *)PyMem_Malloc(keymax * sizeof(*key)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
256 if (key == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
257 goto Done; |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
258 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
259 masklower = PyLong_FromUnsignedLong(0xffffffffU); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
260 if (masklower == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
261 goto Done; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
262 thirtytwo = PyInt_FromLong(32L); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
263 if (thirtytwo == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
264 goto Done; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
265 while ((err=PyObject_IsTrue(n))) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
266 PyObject *newn; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
267 PyObject *pychunk; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
268 unsigned long chunk; |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
269 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
270 if (err == -1) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
271 goto Done; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
272 pychunk = PyNumber_And(n, masklower); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
273 if (pychunk == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
274 goto Done; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
275 chunk = PyLong_AsUnsignedLong(pychunk); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
276 Py_DECREF(pychunk); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
277 if (chunk == (unsigned long)-1 && PyErr_Occurred()) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
278 goto Done; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
279 newn = PyNumber_Rshift(n, thirtytwo); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
280 if (newn == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
281 goto Done; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
282 Py_DECREF(n); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
283 n = newn; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
284 if (keyused >= keymax) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
285 unsigned long bigger = keymax << 1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
286 if ((bigger >> 1) != keymax) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
287 PyErr_NoMemory(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
288 goto Done; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
289 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
290 key = (unsigned long *)PyMem_Realloc(key, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
291 bigger * sizeof(*key)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
292 if (key == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
293 goto Done; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
294 keymax = bigger; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
295 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
296 assert(keyused < keymax); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
297 key[keyused++] = chunk; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
298 } |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
299 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
300 if (keyused == 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
301 key[keyused++] = 0UL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
302 result = init_by_array(self, key, keyused); |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
303 Done: |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
304 Py_XDECREF(masklower); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
305 Py_XDECREF(thirtytwo); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
306 Py_XDECREF(n); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
307 PyMem_Free(key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
308 return result; |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
309 } |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
310 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
311 static PyObject * |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
312 random_getstate(RandomObject *self) |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
313 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
314 PyObject *state; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
315 PyObject *element; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
316 int i; |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
317 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
318 state = PyTuple_New(N+1); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
319 if (state == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
320 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
321 for (i=0; i<N ; i++) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
322 element = PyLong_FromUnsignedLong(self->state[i]); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
323 if (element == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
324 goto Fail; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
325 PyTuple_SET_ITEM(state, i, element); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
326 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
327 element = PyLong_FromLong((long)(self->index)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
328 if (element == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
329 goto Fail; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
330 PyTuple_SET_ITEM(state, i, element); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
331 return state; |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
332 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
333 Fail: |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
334 Py_DECREF(state); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
335 return NULL; |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
336 } |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
337 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
338 static PyObject * |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
339 random_setstate(RandomObject *self, PyObject *state) |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
340 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
341 int i; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
342 unsigned long element; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
343 long index; |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
344 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
345 if (!PyTuple_Check(state)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
346 PyErr_SetString(PyExc_TypeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
347 "state vector must be a tuple"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
348 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
349 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
350 if (PyTuple_Size(state) != N+1) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
351 PyErr_SetString(PyExc_ValueError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
352 "state vector is the wrong size"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
353 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
354 } |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
355 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
356 for (i=0; i<N ; i++) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
357 element = PyLong_AsUnsignedLong(PyTuple_GET_ITEM(state, i)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
358 if (element == (unsigned long)-1 && PyErr_Occurred()) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
359 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
360 self->state[i] = element & 0xffffffffUL; /* Make sure we get sane state */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
361 } |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
362 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
363 index = PyLong_AsLong(PyTuple_GET_ITEM(state, i)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
364 if (index == -1 && PyErr_Occurred()) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
365 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
366 self->index = (int)index; |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
367 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
368 Py_INCREF(Py_None); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
369 return Py_None; |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
370 } |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
371 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
372 /* |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
373 Jumpahead should be a fast way advance the generator n-steps ahead, but |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
374 lacking a formula for that, the next best is to use n and the existing |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
375 state to create a new state far away from the original. |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
376 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
377 The generator uses constant spaced additive feedback, so shuffling the |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
378 state elements ought to produce a state which would not be encountered |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
379 (in the near term) by calls to random(). Shuffling is normally |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
380 implemented by swapping the ith element with another element ranging |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
381 from 0 to i inclusive. That allows the element to have the possibility |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
382 of not being moved. Since the goal is to produce a new, different |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
383 state, the swap element is ranged from 0 to i-1 inclusive. This assures |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
384 that each element gets moved at least once. |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
385 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
386 To make sure that consecutive calls to jumpahead(n) produce different |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
387 states (even in the rare case of involutory shuffles), i+1 is added to |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
388 each element at position i. Successive calls are then guaranteed to |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
389 have changing (growing) values as well as shuffled positions. |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
390 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
391 Finally, the self->index value is set to N so that the generator itself |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
392 kicks in on the next call to random(). This assures that all results |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
393 have been through the generator and do not just reflect alterations to |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
394 the underlying state. |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
395 */ |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
396 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
397 static PyObject * |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
398 random_jumpahead(RandomObject *self, PyObject *n) |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
399 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
400 long i, j; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
401 PyObject *iobj; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
402 PyObject *remobj; |
|
77877
6df0b4ed8617
Closes #14591: Random.jumpahead could produce an invalid MT state on 64-bit machines.
Mark Dickinson <mdickinson@enthought.com>
parents:
64264
diff
changeset
|
403 unsigned long *mt, tmp, nonzero; |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
404 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
405 if (!PyInt_Check(n) && !PyLong_Check(n)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
406 PyErr_Format(PyExc_TypeError, "jumpahead requires an " |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
407 "integer, not '%s'", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
408 Py_TYPE(n)->tp_name); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
409 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
410 } |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
411 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
412 mt = self->state; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
413 for (i = N-1; i > 1; i--) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
414 iobj = PyInt_FromLong(i); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
415 if (iobj == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
416 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
417 remobj = PyNumber_Remainder(n, iobj); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
418 Py_DECREF(iobj); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
419 if (remobj == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
420 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
421 j = PyInt_AsLong(remobj); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
422 Py_DECREF(remobj); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
423 if (j == -1L && PyErr_Occurred()) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
424 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
425 tmp = mt[i]; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
426 mt[i] = mt[j]; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
427 mt[j] = tmp; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
428 } |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
429 |
|
77877
6df0b4ed8617
Closes #14591: Random.jumpahead could produce an invalid MT state on 64-bit machines.
Mark Dickinson <mdickinson@enthought.com>
parents:
64264
diff
changeset
|
430 nonzero = 0; |
|
6df0b4ed8617
Closes #14591: Random.jumpahead could produce an invalid MT state on 64-bit machines.
Mark Dickinson <mdickinson@enthought.com>
parents:
64264
diff
changeset
|
431 for (i = 1; i < N; i++) { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
432 mt[i] += i+1; |
|
77877
6df0b4ed8617
Closes #14591: Random.jumpahead could produce an invalid MT state on 64-bit machines.
Mark Dickinson <mdickinson@enthought.com>
parents:
64264
diff
changeset
|
433 mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */ |
|
6df0b4ed8617
Closes #14591: Random.jumpahead could produce an invalid MT state on 64-bit machines.
Mark Dickinson <mdickinson@enthought.com>
parents:
64264
diff
changeset
|
434 nonzero |= mt[i]; |
|
6df0b4ed8617
Closes #14591: Random.jumpahead could produce an invalid MT state on 64-bit machines.
Mark Dickinson <mdickinson@enthought.com>
parents:
64264
diff
changeset
|
435 } |
|
6df0b4ed8617
Closes #14591: Random.jumpahead could produce an invalid MT state on 64-bit machines.
Mark Dickinson <mdickinson@enthought.com>
parents:
64264
diff
changeset
|
436 |
|
6df0b4ed8617
Closes #14591: Random.jumpahead could produce an invalid MT state on 64-bit machines.
Mark Dickinson <mdickinson@enthought.com>
parents:
64264
diff
changeset
|
437 /* Ensure the state is nonzero: in the unlikely event that mt[1] through |
|
6df0b4ed8617
Closes #14591: Random.jumpahead could produce an invalid MT state on 64-bit machines.
Mark Dickinson <mdickinson@enthought.com>
parents:
64264
diff
changeset
|
438 mt[N-1] are all zero, set the MSB of mt[0] (see issue #14591). In the |
|
6df0b4ed8617
Closes #14591: Random.jumpahead could produce an invalid MT state on 64-bit machines.
Mark Dickinson <mdickinson@enthought.com>
parents:
64264
diff
changeset
|
439 normal case, we fall back to the pre-issue 14591 behaviour for mt[0]. */ |
|
6df0b4ed8617
Closes #14591: Random.jumpahead could produce an invalid MT state on 64-bit machines.
Mark Dickinson <mdickinson@enthought.com>
parents:
64264
diff
changeset
|
440 if (nonzero) { |
|
6df0b4ed8617
Closes #14591: Random.jumpahead could produce an invalid MT state on 64-bit machines.
Mark Dickinson <mdickinson@enthought.com>
parents:
64264
diff
changeset
|
441 mt[0] += 1; |
|
6df0b4ed8617
Closes #14591: Random.jumpahead could produce an invalid MT state on 64-bit machines.
Mark Dickinson <mdickinson@enthought.com>
parents:
64264
diff
changeset
|
442 mt[0] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */ |
|
6df0b4ed8617
Closes #14591: Random.jumpahead could produce an invalid MT state on 64-bit machines.
Mark Dickinson <mdickinson@enthought.com>
parents:
64264
diff
changeset
|
443 } |
|
6df0b4ed8617
Closes #14591: Random.jumpahead could produce an invalid MT state on 64-bit machines.
Mark Dickinson <mdickinson@enthought.com>
parents:
64264
diff
changeset
|
444 else { |
|
6df0b4ed8617
Closes #14591: Random.jumpahead could produce an invalid MT state on 64-bit machines.
Mark Dickinson <mdickinson@enthought.com>
parents:
64264
diff
changeset
|
445 mt[0] = 0x80000000UL; |
|
6df0b4ed8617
Closes #14591: Random.jumpahead could produce an invalid MT state on 64-bit machines.
Mark Dickinson <mdickinson@enthought.com>
parents:
64264
diff
changeset
|
446 } |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
447 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
448 self->index = N; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
449 Py_INCREF(Py_None); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
450 return Py_None; |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
451 } |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
452 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
453 static PyObject * |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
29242
diff
changeset
|
454 random_getrandbits(RandomObject *self, PyObject *args) |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
29242
diff
changeset
|
455 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
456 int k, i, bytes; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
457 unsigned long r; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
458 unsigned char *bytearray; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
459 PyObject *result; |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
29242
diff
changeset
|
460 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
461 if (!PyArg_ParseTuple(args, "i:getrandbits", &k)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
462 return NULL; |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
29242
diff
changeset
|
463 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
464 if (k <= 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
465 PyErr_SetString(PyExc_ValueError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
466 "number of bits must be greater than zero"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
467 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
468 } |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
29242
diff
changeset
|
469 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
470 bytes = ((k - 1) / 32 + 1) * 4; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
471 bytearray = (unsigned char *)PyMem_Malloc(bytes); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
472 if (bytearray == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
473 PyErr_NoMemory(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
474 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
475 } |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
29242
diff
changeset
|
476 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
477 /* Fill-out whole words, byte-by-byte to avoid endianness issues */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
478 for (i=0 ; i<bytes ; i+=4, k-=32) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
479 r = genrand_int32(self); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
480 if (k < 32) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
481 r >>= (32 - k); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
482 bytearray[i+0] = (unsigned char)r; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
483 bytearray[i+1] = (unsigned char)(r >> 8); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
484 bytearray[i+2] = (unsigned char)(r >> 16); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
485 bytearray[i+3] = (unsigned char)(r >> 24); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
486 } |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
29242
diff
changeset
|
487 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
488 /* little endian order to match bytearray assignment order */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
489 result = _PyLong_FromByteArray(bytearray, bytes, 1, 0); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
490 PyMem_Free(bytearray); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
491 return result; |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
29242
diff
changeset
|
492 } |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
29242
diff
changeset
|
493 |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
29242
diff
changeset
|
494 static PyObject * |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
495 random_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
496 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
497 RandomObject *self; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
498 PyObject *tmp; |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
499 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
500 if (type == &Random_Type && !_PyArg_NoKeywords("Random()", kwds)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
501 return NULL; |
|
35450
97c97194e64b
Disallow keyword arguments for type constructors that don't use them
Georg Brandl <georg@python.org>
parents:
30649
diff
changeset
|
502 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
503 self = (RandomObject *)type->tp_alloc(type, 0); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
504 if (self == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
505 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
506 tmp = random_seed(self, args); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
507 if (tmp == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
508 Py_DECREF(self); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
509 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
510 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
511 Py_DECREF(tmp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
512 return (PyObject *)self; |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
513 } |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
514 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
515 static PyMethodDef random_methods[] = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
516 {"random", (PyCFunction)random_random, METH_NOARGS, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
517 PyDoc_STR("random() -> x in the interval [0, 1).")}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
518 {"seed", (PyCFunction)random_seed, METH_VARARGS, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
519 PyDoc_STR("seed([n]) -> None. Defaults to current time.")}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
520 {"getstate", (PyCFunction)random_getstate, METH_NOARGS, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
521 PyDoc_STR("getstate() -> tuple containing the current state.")}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
522 {"setstate", (PyCFunction)random_setstate, METH_O, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
523 PyDoc_STR("setstate(state) -> None. Restores generator state.")}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
524 {"jumpahead", (PyCFunction)random_jumpahead, METH_O, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
525 PyDoc_STR("jumpahead(int) -> None. Create new state from " |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
526 "existing state and integer.")}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
527 {"getrandbits", (PyCFunction)random_getrandbits, METH_VARARGS, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
528 PyDoc_STR("getrandbits(k) -> x. Generates a long int with " |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
529 "k random bits.")}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
530 {NULL, NULL} /* sentinel */ |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
531 }; |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
532 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
533 PyDoc_STRVAR(random_doc, |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
534 "Random() -> create a random number generator with its own internal state."); |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
535 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
536 static PyTypeObject Random_Type = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
537 PyVarObject_HEAD_INIT(NULL, 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
538 "_random.Random", /*tp_name*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
539 sizeof(RandomObject), /*tp_basicsize*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
540 0, /*tp_itemsize*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
541 /* methods */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
542 0, /*tp_dealloc*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
543 0, /*tp_print*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
544 0, /*tp_getattr*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
545 0, /*tp_setattr*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
546 0, /*tp_compare*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
547 0, /*tp_repr*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
548 0, /*tp_as_number*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
549 0, /*tp_as_sequence*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
550 0, /*tp_as_mapping*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
551 0, /*tp_hash*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
552 0, /*tp_call*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
553 0, /*tp_str*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
554 PyObject_GenericGetAttr, /*tp_getattro*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
555 0, /*tp_setattro*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
556 0, /*tp_as_buffer*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
557 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
558 random_doc, /*tp_doc*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
559 0, /*tp_traverse*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
560 0, /*tp_clear*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
561 0, /*tp_richcompare*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
562 0, /*tp_weaklistoffset*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
563 0, /*tp_iter*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
564 0, /*tp_iternext*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
565 random_methods, /*tp_methods*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
566 0, /*tp_members*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
567 0, /*tp_getset*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
568 0, /*tp_base*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
569 0, /*tp_dict*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
570 0, /*tp_descr_get*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
571 0, /*tp_descr_set*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
572 0, /*tp_dictoffset*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
573 0, /*tp_init*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
574 0, /*tp_alloc*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
575 random_new, /*tp_new*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
576 _PyObject_Del, /*tp_free*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
577 0, /*tp_is_gc*/ |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
578 }; |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
579 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
580 PyDoc_STRVAR(module_doc, |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
581 "Module implements the Mersenne Twister random number generator."); |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
582 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
583 PyMODINIT_FUNC |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
584 init_random(void) |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
585 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
586 PyObject *m; |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
587 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
588 if (PyType_Ready(&Random_Type) < 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
589 return; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
590 m = Py_InitModule3("_random", NULL, module_doc); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
591 if (m == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
592 return; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
593 Py_INCREF(&Random_Type); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53799
diff
changeset
|
594 PyModule_AddObject(m, "Random", (PyObject *)&Random_Type); |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
595 } |
