Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 1 | /* Random objects */ |
| 2 | |
| 3 | /* ------------------------------------------------------------------ |
| 4 | The code in this module was based on a download from: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5 | http://www.math.keio.ac.jp/~matumoto/MT2002/emt19937ar.html |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 6 | |
| 7 | It was modified in 2002 by Raymond Hettinger as follows: |
| 8 | |
Benjamin Peterson | d8e5f2d | 2010-08-24 18:08:22 +0000 | [diff] [blame] | 9 | * the principal computational lines untouched. |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 10 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 11 | * renamed genrand_res53() to random_random() and wrapped |
| 12 | in python calling/return code. |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 13 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 14 | * genrand_int32() and the helper functions, init_genrand() |
| 15 | and init_by_array(), were declared static, wrapped in |
| 16 | Python calling/return code. also, their global data |
| 17 | references were replaced with structure references. |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 18 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 19 | * unused functions from the original were deleted. |
| 20 | new, original C python code was added to implement the |
| 21 | Random() interface. |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 22 | |
| 23 | The following are the verbatim comments from the original code: |
| 24 | |
| 25 | A C-program for MT19937, with initialization improved 2002/1/26. |
| 26 | Coded by Takuji Nishimura and Makoto Matsumoto. |
| 27 | |
| 28 | Before using, initialize the state by using init_genrand(seed) |
| 29 | or init_by_array(init_key, key_length). |
| 30 | |
| 31 | Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, |
| 32 | All rights reserved. |
| 33 | |
| 34 | Redistribution and use in source and binary forms, with or without |
| 35 | modification, are permitted provided that the following conditions |
| 36 | are met: |
| 37 | |
| 38 | 1. Redistributions of source code must retain the above copyright |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 39 | notice, this list of conditions and the following disclaimer. |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 40 | |
| 41 | 2. Redistributions in binary form must reproduce the above copyright |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 42 | notice, this list of conditions and the following disclaimer in the |
| 43 | documentation and/or other materials provided with the distribution. |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 44 | |
| 45 | 3. The names of its contributors may not be used to endorse or promote |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 46 | products derived from this software without specific prior written |
| 47 | permission. |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 48 | |
| 49 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 50 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 51 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 52 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 53 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 54 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 55 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 56 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 57 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 58 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 59 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 60 | |
| 61 | |
| 62 | Any feedback is very welcome. |
| 63 | http://www.math.keio.ac.jp/matumoto/emt.html |
| 64 | email: matumoto@math.keio.ac.jp |
| 65 | */ |
| 66 | |
| 67 | /* ---------------------------------------------------------------*/ |
| 68 | |
| 69 | #include "Python.h" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 70 | #include <time.h> /* for seeding to current time */ |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 71 | |
| 72 | /* Period parameters -- These are all magic. Don't change. */ |
| 73 | #define N 624 |
| 74 | #define M 397 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 75 | #define MATRIX_A 0x9908b0dfUL /* constant vector a */ |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 76 | #define UPPER_MASK 0x80000000UL /* most significant w-r bits */ |
| 77 | #define LOWER_MASK 0x7fffffffUL /* least significant r bits */ |
| 78 | |
| 79 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 80 | PyObject_HEAD |
| 81 | unsigned long state[N]; |
| 82 | int index; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 83 | } RandomObject; |
| 84 | |
| 85 | static PyTypeObject Random_Type; |
| 86 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 87 | #define RandomObject_Check(v) (Py_TYPE(v) == &Random_Type) |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 88 | |
| 89 | |
| 90 | /* Random methods */ |
| 91 | |
| 92 | |
| 93 | /* generates a random number on [0,0xffffffff]-interval */ |
| 94 | static unsigned long |
| 95 | genrand_int32(RandomObject *self) |
| 96 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 97 | unsigned long y; |
| 98 | static unsigned long mag01[2]={0x0UL, MATRIX_A}; |
| 99 | /* mag01[x] = x * MATRIX_A for x=0,1 */ |
| 100 | unsigned long *mt; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 101 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 102 | mt = self->state; |
| 103 | if (self->index >= N) { /* generate N words at one time */ |
| 104 | int kk; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 105 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 106 | for (kk=0;kk<N-M;kk++) { |
| 107 | y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK); |
| 108 | mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL]; |
| 109 | } |
| 110 | for (;kk<N-1;kk++) { |
| 111 | y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK); |
| 112 | mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL]; |
| 113 | } |
| 114 | y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK); |
| 115 | mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL]; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 116 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 117 | self->index = 0; |
| 118 | } |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 119 | |
| 120 | y = mt[self->index++]; |
| 121 | y ^= (y >> 11); |
| 122 | y ^= (y << 7) & 0x9d2c5680UL; |
| 123 | y ^= (y << 15) & 0xefc60000UL; |
| 124 | y ^= (y >> 18); |
| 125 | return y; |
| 126 | } |
| 127 | |
| 128 | /* random_random is the function named genrand_res53 in the original code; |
| 129 | * generates a random number on [0,1) with 53-bit resolution; note that |
| 130 | * 9007199254740992 == 2**53; I assume they're spelling "/2**53" as |
| 131 | * multiply-by-reciprocal in the (likely vain) hope that the compiler will |
| 132 | * optimize the division away at compile-time. 67108864 is 2**26. In |
| 133 | * effect, a contains 27 random bits shifted left 26, and b fills in the |
| 134 | * lower 26 bits of the 53-bit numerator. |
| 135 | * The orginal code credited Isaku Wada for this algorithm, 2002/01/09. |
| 136 | */ |
| 137 | static PyObject * |
| 138 | random_random(RandomObject *self) |
| 139 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 140 | unsigned long a=genrand_int32(self)>>5, b=genrand_int32(self)>>6; |
| 141 | return PyFloat_FromDouble((a*67108864.0+b)*(1.0/9007199254740992.0)); |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /* initializes mt[N] with a seed */ |
| 145 | static void |
| 146 | init_genrand(RandomObject *self, unsigned long s) |
| 147 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 148 | int mti; |
| 149 | unsigned long *mt; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 150 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 151 | mt = self->state; |
| 152 | mt[0]= s & 0xffffffffUL; |
| 153 | for (mti=1; mti<N; mti++) { |
| 154 | mt[mti] = |
| 155 | (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti); |
| 156 | /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */ |
| 157 | /* In the previous versions, MSBs of the seed affect */ |
| 158 | /* only MSBs of the array mt[]. */ |
| 159 | /* 2002/01/09 modified by Makoto Matsumoto */ |
| 160 | mt[mti] &= 0xffffffffUL; |
| 161 | /* for >32 bit machines */ |
| 162 | } |
| 163 | self->index = mti; |
| 164 | return; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /* initialize by an array with array-length */ |
| 168 | /* init_key is the array for initializing keys */ |
| 169 | /* key_length is its length */ |
| 170 | static PyObject * |
| 171 | init_by_array(RandomObject *self, unsigned long init_key[], unsigned long key_length) |
| 172 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 173 | unsigned int i, j, k; /* was signed in the original code. RDH 12/16/2002 */ |
| 174 | unsigned long *mt; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 175 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 176 | mt = self->state; |
| 177 | init_genrand(self, 19650218UL); |
| 178 | i=1; j=0; |
| 179 | k = (N>key_length ? N : key_length); |
| 180 | for (; k; k--) { |
| 181 | mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL)) |
| 182 | + init_key[j] + j; /* non linear */ |
| 183 | mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */ |
| 184 | i++; j++; |
| 185 | if (i>=N) { mt[0] = mt[N-1]; i=1; } |
| 186 | if (j>=key_length) j=0; |
| 187 | } |
| 188 | for (k=N-1; k; k--) { |
| 189 | mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL)) |
| 190 | - i; /* non linear */ |
| 191 | mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */ |
| 192 | i++; |
| 193 | if (i>=N) { mt[0] = mt[N-1]; i=1; } |
| 194 | } |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 195 | |
| 196 | mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */ |
| 197 | Py_INCREF(Py_None); |
| 198 | return Py_None; |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | * The rest is Python-specific code, neither part of, nor derived from, the |
| 203 | * Twister download. |
| 204 | */ |
| 205 | |
| 206 | static PyObject * |
| 207 | random_seed(RandomObject *self, PyObject *args) |
| 208 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 209 | PyObject *result = NULL; /* guilty until proved innocent */ |
| 210 | PyObject *masklower = NULL; |
| 211 | PyObject *thirtytwo = NULL; |
| 212 | PyObject *n = NULL; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 213 | unsigned long *new_key, *key = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 214 | unsigned long keymax; /* # of allocated slots in key */ |
| 215 | unsigned long keyused; /* # of used slots in key */ |
| 216 | int err; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 217 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 218 | PyObject *arg = NULL; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 219 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 220 | if (!PyArg_UnpackTuple(args, "seed", 0, 1, &arg)) |
| 221 | return NULL; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 222 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 223 | if (arg == NULL || arg == Py_None) { |
| 224 | time_t now; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 225 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 226 | time(&now); |
| 227 | init_genrand(self, (unsigned long)now); |
| 228 | Py_INCREF(Py_None); |
| 229 | return Py_None; |
| 230 | } |
Larry Hastings | d60cd42 | 2012-06-24 02:52:21 -0700 | [diff] [blame] | 231 | /* This algorithm relies on the number being unsigned. |
| 232 | * So: if the arg is a PyLong, use its absolute value. |
| 233 | * Otherwise use its hash value, cast to unsigned. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 234 | */ |
| 235 | if (PyLong_Check(arg)) |
| 236 | n = PyNumber_Absolute(arg); |
| 237 | else { |
Larry Hastings | d60cd42 | 2012-06-24 02:52:21 -0700 | [diff] [blame] | 238 | Py_hash_t hash = PyObject_Hash(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 239 | if (hash == -1) |
| 240 | goto Done; |
Larry Hastings | d60cd42 | 2012-06-24 02:52:21 -0700 | [diff] [blame] | 241 | n = PyLong_FromSize_t((size_t)hash); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 242 | } |
| 243 | if (n == NULL) |
| 244 | goto Done; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 245 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 246 | /* Now split n into 32-bit chunks, from the right. Each piece is |
| 247 | * stored into key, which has a capacity of keymax chunks, of which |
| 248 | * keyused are filled. Alas, the repeated shifting makes this a |
| 249 | * quadratic-time algorithm; we'd really like to use |
| 250 | * _PyLong_AsByteArray here, but then we'd have to break into the |
| 251 | * long representation to figure out how big an array was needed |
| 252 | * in advance. |
| 253 | */ |
| 254 | keymax = 8; /* arbitrary; grows later if needed */ |
| 255 | keyused = 0; |
| 256 | key = (unsigned long *)PyMem_Malloc(keymax * sizeof(*key)); |
| 257 | if (key == NULL) |
| 258 | goto Done; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 259 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 260 | masklower = PyLong_FromUnsignedLong(0xffffffffU); |
| 261 | if (masklower == NULL) |
| 262 | goto Done; |
| 263 | thirtytwo = PyLong_FromLong(32L); |
| 264 | if (thirtytwo == NULL) |
| 265 | goto Done; |
| 266 | while ((err=PyObject_IsTrue(n))) { |
| 267 | PyObject *newn; |
| 268 | PyObject *pychunk; |
| 269 | unsigned long chunk; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 270 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 271 | if (err == -1) |
| 272 | goto Done; |
| 273 | pychunk = PyNumber_And(n, masklower); |
| 274 | if (pychunk == NULL) |
| 275 | goto Done; |
| 276 | chunk = PyLong_AsUnsignedLong(pychunk); |
| 277 | Py_DECREF(pychunk); |
| 278 | if (chunk == (unsigned long)-1 && PyErr_Occurred()) |
| 279 | goto Done; |
| 280 | newn = PyNumber_Rshift(n, thirtytwo); |
| 281 | if (newn == NULL) |
| 282 | goto Done; |
| 283 | Py_DECREF(n); |
| 284 | n = newn; |
| 285 | if (keyused >= keymax) { |
| 286 | unsigned long bigger = keymax << 1; |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame^] | 287 | if ((bigger >> 1) != keymax || |
| 288 | bigger > PY_SSIZE_T_MAX / sizeof(*key)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 289 | PyErr_NoMemory(); |
| 290 | goto Done; |
| 291 | } |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 292 | new_key = (unsigned long *)PyMem_Realloc(key, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 293 | bigger * sizeof(*key)); |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 294 | if (new_key == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 295 | goto Done; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 296 | key = new_key; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 297 | keymax = bigger; |
| 298 | } |
| 299 | assert(keyused < keymax); |
| 300 | key[keyused++] = chunk; |
| 301 | } |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 302 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 303 | if (keyused == 0) |
| 304 | key[keyused++] = 0UL; |
| 305 | result = init_by_array(self, key, keyused); |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 306 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 307 | Py_XDECREF(masklower); |
| 308 | Py_XDECREF(thirtytwo); |
| 309 | Py_XDECREF(n); |
| 310 | PyMem_Free(key); |
| 311 | return result; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | static PyObject * |
| 315 | random_getstate(RandomObject *self) |
| 316 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 317 | PyObject *state; |
| 318 | PyObject *element; |
| 319 | int i; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 320 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 321 | state = PyTuple_New(N+1); |
| 322 | if (state == NULL) |
| 323 | return NULL; |
| 324 | for (i=0; i<N ; i++) { |
| 325 | element = PyLong_FromUnsignedLong(self->state[i]); |
| 326 | if (element == NULL) |
| 327 | goto Fail; |
| 328 | PyTuple_SET_ITEM(state, i, element); |
| 329 | } |
| 330 | element = PyLong_FromLong((long)(self->index)); |
| 331 | if (element == NULL) |
| 332 | goto Fail; |
| 333 | PyTuple_SET_ITEM(state, i, element); |
| 334 | return state; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 335 | |
| 336 | Fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 337 | Py_DECREF(state); |
| 338 | return NULL; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | static PyObject * |
| 342 | random_setstate(RandomObject *self, PyObject *state) |
| 343 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 344 | int i; |
| 345 | unsigned long element; |
| 346 | long index; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 347 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 348 | if (!PyTuple_Check(state)) { |
| 349 | PyErr_SetString(PyExc_TypeError, |
| 350 | "state vector must be a tuple"); |
| 351 | return NULL; |
| 352 | } |
| 353 | if (PyTuple_Size(state) != N+1) { |
| 354 | PyErr_SetString(PyExc_ValueError, |
| 355 | "state vector is the wrong size"); |
| 356 | return NULL; |
| 357 | } |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 358 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 359 | for (i=0; i<N ; i++) { |
| 360 | element = PyLong_AsUnsignedLong(PyTuple_GET_ITEM(state, i)); |
| 361 | if (element == (unsigned long)-1 && PyErr_Occurred()) |
| 362 | return NULL; |
| 363 | self->state[i] = element & 0xffffffffUL; /* Make sure we get sane state */ |
| 364 | } |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 365 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 366 | index = PyLong_AsLong(PyTuple_GET_ITEM(state, i)); |
| 367 | if (index == -1 && PyErr_Occurred()) |
| 368 | return NULL; |
| 369 | self->index = (int)index; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 370 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 371 | Py_INCREF(Py_None); |
| 372 | return Py_None; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 375 | static PyObject * |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 376 | random_getrandbits(RandomObject *self, PyObject *args) |
| 377 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 378 | int k, i, bytes; |
| 379 | unsigned long r; |
| 380 | unsigned char *bytearray; |
| 381 | PyObject *result; |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 382 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 383 | if (!PyArg_ParseTuple(args, "i:getrandbits", &k)) |
| 384 | return NULL; |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 385 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 386 | if (k <= 0) { |
| 387 | PyErr_SetString(PyExc_ValueError, |
| 388 | "number of bits must be greater than zero"); |
| 389 | return NULL; |
| 390 | } |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 391 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 392 | bytes = ((k - 1) / 32 + 1) * 4; |
| 393 | bytearray = (unsigned char *)PyMem_Malloc(bytes); |
| 394 | if (bytearray == NULL) { |
| 395 | PyErr_NoMemory(); |
| 396 | return NULL; |
| 397 | } |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 398 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 399 | /* Fill-out whole words, byte-by-byte to avoid endianness issues */ |
| 400 | for (i=0 ; i<bytes ; i+=4, k-=32) { |
| 401 | r = genrand_int32(self); |
| 402 | if (k < 32) |
| 403 | r >>= (32 - k); |
| 404 | bytearray[i+0] = (unsigned char)r; |
| 405 | bytearray[i+1] = (unsigned char)(r >> 8); |
| 406 | bytearray[i+2] = (unsigned char)(r >> 16); |
| 407 | bytearray[i+3] = (unsigned char)(r >> 24); |
| 408 | } |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 409 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 410 | /* little endian order to match bytearray assignment order */ |
| 411 | result = _PyLong_FromByteArray(bytearray, bytes, 1, 0); |
| 412 | PyMem_Free(bytearray); |
| 413 | return result; |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | static PyObject * |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 417 | random_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 418 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 419 | RandomObject *self; |
| 420 | PyObject *tmp; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 421 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 422 | if (type == &Random_Type && !_PyArg_NoKeywords("Random()", kwds)) |
| 423 | return NULL; |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 424 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 425 | self = (RandomObject *)type->tp_alloc(type, 0); |
| 426 | if (self == NULL) |
| 427 | return NULL; |
| 428 | tmp = random_seed(self, args); |
| 429 | if (tmp == NULL) { |
| 430 | Py_DECREF(self); |
| 431 | return NULL; |
| 432 | } |
| 433 | Py_DECREF(tmp); |
| 434 | return (PyObject *)self; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | static PyMethodDef random_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 438 | {"random", (PyCFunction)random_random, METH_NOARGS, |
| 439 | PyDoc_STR("random() -> x in the interval [0, 1).")}, |
| 440 | {"seed", (PyCFunction)random_seed, METH_VARARGS, |
| 441 | PyDoc_STR("seed([n]) -> None. Defaults to current time.")}, |
| 442 | {"getstate", (PyCFunction)random_getstate, METH_NOARGS, |
| 443 | PyDoc_STR("getstate() -> tuple containing the current state.")}, |
| 444 | {"setstate", (PyCFunction)random_setstate, METH_O, |
| 445 | PyDoc_STR("setstate(state) -> None. Restores generator state.")}, |
| 446 | {"getrandbits", (PyCFunction)random_getrandbits, METH_VARARGS, |
| 447 | PyDoc_STR("getrandbits(k) -> x. Generates a long int with " |
| 448 | "k random bits.")}, |
| 449 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 450 | }; |
| 451 | |
| 452 | PyDoc_STRVAR(random_doc, |
| 453 | "Random() -> create a random number generator with its own internal state."); |
| 454 | |
| 455 | static PyTypeObject Random_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 456 | PyVarObject_HEAD_INIT(NULL, 0) |
| 457 | "_random.Random", /*tp_name*/ |
| 458 | sizeof(RandomObject), /*tp_basicsize*/ |
| 459 | 0, /*tp_itemsize*/ |
| 460 | /* methods */ |
| 461 | 0, /*tp_dealloc*/ |
| 462 | 0, /*tp_print*/ |
| 463 | 0, /*tp_getattr*/ |
| 464 | 0, /*tp_setattr*/ |
| 465 | 0, /*tp_reserved*/ |
| 466 | 0, /*tp_repr*/ |
| 467 | 0, /*tp_as_number*/ |
| 468 | 0, /*tp_as_sequence*/ |
| 469 | 0, /*tp_as_mapping*/ |
| 470 | 0, /*tp_hash*/ |
| 471 | 0, /*tp_call*/ |
| 472 | 0, /*tp_str*/ |
| 473 | PyObject_GenericGetAttr, /*tp_getattro*/ |
| 474 | 0, /*tp_setattro*/ |
| 475 | 0, /*tp_as_buffer*/ |
| 476 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
| 477 | random_doc, /*tp_doc*/ |
| 478 | 0, /*tp_traverse*/ |
| 479 | 0, /*tp_clear*/ |
| 480 | 0, /*tp_richcompare*/ |
| 481 | 0, /*tp_weaklistoffset*/ |
| 482 | 0, /*tp_iter*/ |
| 483 | 0, /*tp_iternext*/ |
| 484 | random_methods, /*tp_methods*/ |
| 485 | 0, /*tp_members*/ |
| 486 | 0, /*tp_getset*/ |
| 487 | 0, /*tp_base*/ |
| 488 | 0, /*tp_dict*/ |
| 489 | 0, /*tp_descr_get*/ |
| 490 | 0, /*tp_descr_set*/ |
| 491 | 0, /*tp_dictoffset*/ |
| 492 | 0, /*tp_init*/ |
| 493 | 0, /*tp_alloc*/ |
| 494 | random_new, /*tp_new*/ |
| 495 | PyObject_Free, /*tp_free*/ |
| 496 | 0, /*tp_is_gc*/ |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 497 | }; |
| 498 | |
| 499 | PyDoc_STRVAR(module_doc, |
| 500 | "Module implements the Mersenne Twister random number generator."); |
| 501 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 502 | |
| 503 | static struct PyModuleDef _randommodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 504 | PyModuleDef_HEAD_INIT, |
| 505 | "_random", |
| 506 | module_doc, |
| 507 | -1, |
| 508 | NULL, |
| 509 | NULL, |
| 510 | NULL, |
| 511 | NULL, |
| 512 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 513 | }; |
| 514 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 515 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 516 | PyInit__random(void) |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 517 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 518 | PyObject *m; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 519 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 520 | if (PyType_Ready(&Random_Type) < 0) |
| 521 | return NULL; |
| 522 | m = PyModule_Create(&_randommodule); |
| 523 | if (m == NULL) |
| 524 | return NULL; |
| 525 | Py_INCREF(&Random_Type); |
| 526 | PyModule_AddObject(m, "Random", (PyObject *)&Random_Type); |
| 527 | return m; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 528 | } |