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 | |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 72 | #ifndef PY_UINT32_T |
| 73 | # error "Failed to find an exact-width 32-bit integer type" |
| 74 | #endif |
| 75 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 76 | /* Period parameters -- These are all magic. Don't change. */ |
| 77 | #define N 624 |
| 78 | #define M 397 |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 79 | #define MATRIX_A 0x9908b0dfU /* constant vector a */ |
| 80 | #define UPPER_MASK 0x80000000U /* most significant w-r bits */ |
| 81 | #define LOWER_MASK 0x7fffffffU /* least significant r bits */ |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 82 | |
| 83 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 84 | PyObject_HEAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 85 | int index; |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 86 | PY_UINT32_T state[N]; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 87 | } RandomObject; |
| 88 | |
| 89 | static PyTypeObject Random_Type; |
| 90 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 91 | #define RandomObject_Check(v) (Py_TYPE(v) == &Random_Type) |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 92 | |
| 93 | |
| 94 | /* Random methods */ |
| 95 | |
| 96 | |
| 97 | /* generates a random number on [0,0xffffffff]-interval */ |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 98 | static PY_UINT32_T |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 99 | genrand_int32(RandomObject *self) |
| 100 | { |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 101 | PY_UINT32_T y; |
| 102 | static PY_UINT32_T mag01[2]={0x0U, MATRIX_A}; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 103 | /* mag01[x] = x * MATRIX_A for x=0,1 */ |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 104 | PY_UINT32_T *mt; |
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 | mt = self->state; |
| 107 | if (self->index >= N) { /* generate N words at one time */ |
| 108 | int kk; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 109 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 110 | for (kk=0;kk<N-M;kk++) { |
| 111 | y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK); |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 112 | mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1U]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 113 | } |
| 114 | for (;kk<N-1;kk++) { |
| 115 | y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK); |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 116 | mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1U]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 117 | } |
| 118 | y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK); |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 119 | mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1U]; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 120 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 121 | self->index = 0; |
| 122 | } |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 123 | |
| 124 | y = mt[self->index++]; |
| 125 | y ^= (y >> 11); |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 126 | y ^= (y << 7) & 0x9d2c5680U; |
| 127 | y ^= (y << 15) & 0xefc60000U; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 128 | y ^= (y >> 18); |
| 129 | return y; |
| 130 | } |
| 131 | |
| 132 | /* random_random is the function named genrand_res53 in the original code; |
| 133 | * generates a random number on [0,1) with 53-bit resolution; note that |
| 134 | * 9007199254740992 == 2**53; I assume they're spelling "/2**53" as |
| 135 | * multiply-by-reciprocal in the (likely vain) hope that the compiler will |
| 136 | * optimize the division away at compile-time. 67108864 is 2**26. In |
| 137 | * effect, a contains 27 random bits shifted left 26, and b fills in the |
| 138 | * lower 26 bits of the 53-bit numerator. |
| 139 | * The orginal code credited Isaku Wada for this algorithm, 2002/01/09. |
| 140 | */ |
| 141 | static PyObject * |
| 142 | random_random(RandomObject *self) |
| 143 | { |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 144 | PY_UINT32_T a=genrand_int32(self)>>5, b=genrand_int32(self)>>6; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 145 | return PyFloat_FromDouble((a*67108864.0+b)*(1.0/9007199254740992.0)); |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /* initializes mt[N] with a seed */ |
| 149 | static void |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 150 | init_genrand(RandomObject *self, PY_UINT32_T s) |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 151 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 152 | int mti; |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 153 | PY_UINT32_T *mt; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 154 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 155 | mt = self->state; |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 156 | mt[0]= s; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 157 | for (mti=1; mti<N; mti++) { |
| 158 | mt[mti] = |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 159 | (1812433253U * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 160 | /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */ |
| 161 | /* In the previous versions, MSBs of the seed affect */ |
| 162 | /* only MSBs of the array mt[]. */ |
| 163 | /* 2002/01/09 modified by Makoto Matsumoto */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 164 | } |
| 165 | self->index = mti; |
| 166 | return; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /* initialize by an array with array-length */ |
| 170 | /* init_key is the array for initializing keys */ |
| 171 | /* key_length is its length */ |
| 172 | static PyObject * |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 173 | init_by_array(RandomObject *self, PY_UINT32_T init_key[], size_t key_length) |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 174 | { |
Mark Dickinson | 4cd6017 | 2012-12-21 21:52:49 +0000 | [diff] [blame] | 175 | size_t i, j, k; /* was signed in the original code. RDH 12/16/2002 */ |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 176 | PY_UINT32_T *mt; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 177 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 178 | mt = self->state; |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 179 | init_genrand(self, 19650218U); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 180 | i=1; j=0; |
| 181 | k = (N>key_length ? N : key_length); |
| 182 | for (; k; k--) { |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 183 | mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525U)) |
| 184 | + init_key[j] + (PY_UINT32_T)j; /* non linear */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 185 | i++; j++; |
| 186 | if (i>=N) { mt[0] = mt[N-1]; i=1; } |
| 187 | if (j>=key_length) j=0; |
| 188 | } |
| 189 | for (k=N-1; k; k--) { |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 190 | mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941U)) |
| 191 | - (PY_UINT32_T)i; /* non linear */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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 | |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 196 | mt[0] = 0x80000000U; /* MSB is 1; assuring non-zero initial array */ |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 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 */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 210 | PyObject *n = NULL; |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 211 | PY_UINT32_T *key = NULL; |
| 212 | size_t bits, keyused; |
Mark Dickinson | 4cd6017 | 2012-12-21 21:52:49 +0000 | [diff] [blame] | 213 | int res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 214 | PyObject *arg = NULL; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 215 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 216 | if (!PyArg_UnpackTuple(args, "seed", 0, 1, &arg)) |
| 217 | return NULL; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 218 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 219 | if (arg == NULL || arg == Py_None) { |
| 220 | time_t now; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 221 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 222 | time(&now); |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 223 | init_genrand(self, (PY_UINT32_T)now); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 224 | Py_INCREF(Py_None); |
| 225 | return Py_None; |
| 226 | } |
Larry Hastings | d60cd42 | 2012-06-24 02:52:21 -0700 | [diff] [blame] | 227 | /* This algorithm relies on the number being unsigned. |
| 228 | * So: if the arg is a PyLong, use its absolute value. |
| 229 | * Otherwise use its hash value, cast to unsigned. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 230 | */ |
| 231 | if (PyLong_Check(arg)) |
| 232 | n = PyNumber_Absolute(arg); |
| 233 | else { |
Larry Hastings | d60cd42 | 2012-06-24 02:52:21 -0700 | [diff] [blame] | 234 | Py_hash_t hash = PyObject_Hash(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 235 | if (hash == -1) |
| 236 | goto Done; |
Larry Hastings | d60cd42 | 2012-06-24 02:52:21 -0700 | [diff] [blame] | 237 | n = PyLong_FromSize_t((size_t)hash); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 238 | } |
| 239 | if (n == NULL) |
| 240 | goto Done; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 241 | |
Mark Dickinson | 4cd6017 | 2012-12-21 21:52:49 +0000 | [diff] [blame] | 242 | /* Now split n into 32-bit chunks, from the right. */ |
| 243 | bits = _PyLong_NumBits(n); |
| 244 | if (bits == (size_t)-1 && PyErr_Occurred()) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 245 | goto Done; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 246 | |
Mark Dickinson | 4cd6017 | 2012-12-21 21:52:49 +0000 | [diff] [blame] | 247 | /* Figure out how many 32-bit chunks this gives us. */ |
| 248 | keyused = bits == 0 ? 1 : (bits - 1) / 32 + 1; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 249 | |
Mark Dickinson | 4cd6017 | 2012-12-21 21:52:49 +0000 | [diff] [blame] | 250 | /* Convert seed to byte sequence. */ |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 251 | key = (PY_UINT32_T *)PyMem_Malloc((size_t)4 * keyused); |
| 252 | if (key == NULL) { |
Victor Stinner | a4ced86 | 2013-07-15 20:00:36 +0200 | [diff] [blame] | 253 | PyErr_NoMemory(); |
Mark Dickinson | 4cd6017 | 2012-12-21 21:52:49 +0000 | [diff] [blame] | 254 | goto Done; |
Victor Stinner | a4ced86 | 2013-07-15 20:00:36 +0200 | [diff] [blame] | 255 | } |
Mark Dickinson | 4cd6017 | 2012-12-21 21:52:49 +0000 | [diff] [blame] | 256 | res = _PyLong_AsByteArray((PyLongObject *)n, |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 257 | (unsigned char *)key, keyused * 4, |
| 258 | PY_LITTLE_ENDIAN, |
Mark Dickinson | 4cd6017 | 2012-12-21 21:52:49 +0000 | [diff] [blame] | 259 | 0); /* unsigned */ |
| 260 | if (res == -1) { |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 261 | PyMem_Free(key); |
Mark Dickinson | 4cd6017 | 2012-12-21 21:52:49 +0000 | [diff] [blame] | 262 | goto Done; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 263 | } |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 264 | |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 265 | #if PY_BIG_ENDIAN |
| 266 | { |
| 267 | size_t i, j; |
| 268 | /* Reverse an array. */ |
Zachary Ware | c15ea4c | 2015-05-17 23:46:22 -0500 | [diff] [blame] | 269 | for (i = 0, j = keyused - 1; i < j; i++, j--) { |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 270 | PY_UINT32_T tmp = key[i]; |
| 271 | key[i] = key[j]; |
| 272 | key[j] = tmp; |
| 273 | } |
Mark Dickinson | 4cd6017 | 2012-12-21 21:52:49 +0000 | [diff] [blame] | 274 | } |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 275 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 276 | result = init_by_array(self, key, keyused); |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 277 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 278 | Py_XDECREF(n); |
| 279 | PyMem_Free(key); |
| 280 | return result; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | static PyObject * |
| 284 | random_getstate(RandomObject *self) |
| 285 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 286 | PyObject *state; |
| 287 | PyObject *element; |
| 288 | int i; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 289 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 290 | state = PyTuple_New(N+1); |
| 291 | if (state == NULL) |
| 292 | return NULL; |
| 293 | for (i=0; i<N ; i++) { |
| 294 | element = PyLong_FromUnsignedLong(self->state[i]); |
| 295 | if (element == NULL) |
| 296 | goto Fail; |
| 297 | PyTuple_SET_ITEM(state, i, element); |
| 298 | } |
| 299 | element = PyLong_FromLong((long)(self->index)); |
| 300 | if (element == NULL) |
| 301 | goto Fail; |
| 302 | PyTuple_SET_ITEM(state, i, element); |
| 303 | return state; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 304 | |
| 305 | Fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 306 | Py_DECREF(state); |
| 307 | return NULL; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | static PyObject * |
| 311 | random_setstate(RandomObject *self, PyObject *state) |
| 312 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 313 | int i; |
| 314 | unsigned long element; |
| 315 | long index; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 316 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 317 | if (!PyTuple_Check(state)) { |
| 318 | PyErr_SetString(PyExc_TypeError, |
| 319 | "state vector must be a tuple"); |
| 320 | return NULL; |
| 321 | } |
| 322 | if (PyTuple_Size(state) != N+1) { |
| 323 | PyErr_SetString(PyExc_ValueError, |
| 324 | "state vector is the wrong size"); |
| 325 | return NULL; |
| 326 | } |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 327 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 328 | for (i=0; i<N ; i++) { |
| 329 | element = PyLong_AsUnsignedLong(PyTuple_GET_ITEM(state, i)); |
| 330 | if (element == (unsigned long)-1 && PyErr_Occurred()) |
| 331 | return NULL; |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 332 | self->state[i] = (PY_UINT32_T)element; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 333 | } |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 334 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 335 | index = PyLong_AsLong(PyTuple_GET_ITEM(state, i)); |
| 336 | if (index == -1 && PyErr_Occurred()) |
| 337 | return NULL; |
Serhiy Storchaka | 178f0b6 | 2015-07-24 09:02:53 +0300 | [diff] [blame] | 338 | if (index < 0 || index > N) { |
| 339 | PyErr_SetString(PyExc_ValueError, "invalid state"); |
| 340 | return NULL; |
| 341 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 342 | self->index = (int)index; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 343 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 344 | Py_INCREF(Py_None); |
| 345 | return Py_None; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 348 | static PyObject * |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 349 | random_getrandbits(RandomObject *self, PyObject *args) |
| 350 | { |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 351 | int k, i, words; |
| 352 | PY_UINT32_T r; |
| 353 | PY_UINT32_T *wordarray; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 354 | PyObject *result; |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 355 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 356 | if (!PyArg_ParseTuple(args, "i:getrandbits", &k)) |
| 357 | return NULL; |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 358 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 359 | if (k <= 0) { |
| 360 | PyErr_SetString(PyExc_ValueError, |
| 361 | "number of bits must be greater than zero"); |
| 362 | return NULL; |
| 363 | } |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 364 | |
Serhiy Storchaka | d8a0bac | 2013-01-04 12:18:35 +0200 | [diff] [blame] | 365 | if (k <= 32) /* Fast path */ |
| 366 | return PyLong_FromUnsignedLong(genrand_int32(self) >> (32 - k)); |
| 367 | |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 368 | words = (k - 1) / 32 + 1; |
| 369 | wordarray = (PY_UINT32_T *)PyMem_Malloc(words * 4); |
| 370 | if (wordarray == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 371 | PyErr_NoMemory(); |
| 372 | return NULL; |
| 373 | } |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 374 | |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 375 | /* Fill-out bits of long integer, by 32-bit words, from least significant |
| 376 | to most significant. */ |
| 377 | #if PY_LITTLE_ENDIAN |
| 378 | for (i = 0; i < words; i++, k -= 32) |
| 379 | #else |
| 380 | for (i = words - 1; i >= 0; i--, k -= 32) |
| 381 | #endif |
| 382 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 383 | r = genrand_int32(self); |
| 384 | if (k < 32) |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 385 | r >>= (32 - k); /* Drop least significant bits */ |
| 386 | wordarray[i] = r; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 387 | } |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 388 | |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 389 | result = _PyLong_FromByteArray((unsigned char *)wordarray, words * 4, |
| 390 | PY_LITTLE_ENDIAN, 0 /* unsigned */); |
| 391 | PyMem_Free(wordarray); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 392 | return result; |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | static PyObject * |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 396 | random_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 397 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 398 | RandomObject *self; |
| 399 | PyObject *tmp; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 400 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 401 | if (type == &Random_Type && !_PyArg_NoKeywords("Random()", kwds)) |
| 402 | return NULL; |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 403 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 404 | self = (RandomObject *)type->tp_alloc(type, 0); |
| 405 | if (self == NULL) |
| 406 | return NULL; |
| 407 | tmp = random_seed(self, args); |
| 408 | if (tmp == NULL) { |
| 409 | Py_DECREF(self); |
| 410 | return NULL; |
| 411 | } |
| 412 | Py_DECREF(tmp); |
| 413 | return (PyObject *)self; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | static PyMethodDef random_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 417 | {"random", (PyCFunction)random_random, METH_NOARGS, |
| 418 | PyDoc_STR("random() -> x in the interval [0, 1).")}, |
| 419 | {"seed", (PyCFunction)random_seed, METH_VARARGS, |
| 420 | PyDoc_STR("seed([n]) -> None. Defaults to current time.")}, |
| 421 | {"getstate", (PyCFunction)random_getstate, METH_NOARGS, |
| 422 | PyDoc_STR("getstate() -> tuple containing the current state.")}, |
| 423 | {"setstate", (PyCFunction)random_setstate, METH_O, |
| 424 | PyDoc_STR("setstate(state) -> None. Restores generator state.")}, |
| 425 | {"getrandbits", (PyCFunction)random_getrandbits, METH_VARARGS, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 426 | PyDoc_STR("getrandbits(k) -> x. Generates an int with " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 427 | "k random bits.")}, |
| 428 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 429 | }; |
| 430 | |
| 431 | PyDoc_STRVAR(random_doc, |
| 432 | "Random() -> create a random number generator with its own internal state."); |
| 433 | |
| 434 | static PyTypeObject Random_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 435 | PyVarObject_HEAD_INIT(NULL, 0) |
| 436 | "_random.Random", /*tp_name*/ |
| 437 | sizeof(RandomObject), /*tp_basicsize*/ |
| 438 | 0, /*tp_itemsize*/ |
| 439 | /* methods */ |
| 440 | 0, /*tp_dealloc*/ |
| 441 | 0, /*tp_print*/ |
| 442 | 0, /*tp_getattr*/ |
| 443 | 0, /*tp_setattr*/ |
| 444 | 0, /*tp_reserved*/ |
| 445 | 0, /*tp_repr*/ |
| 446 | 0, /*tp_as_number*/ |
| 447 | 0, /*tp_as_sequence*/ |
| 448 | 0, /*tp_as_mapping*/ |
| 449 | 0, /*tp_hash*/ |
| 450 | 0, /*tp_call*/ |
| 451 | 0, /*tp_str*/ |
| 452 | PyObject_GenericGetAttr, /*tp_getattro*/ |
| 453 | 0, /*tp_setattro*/ |
| 454 | 0, /*tp_as_buffer*/ |
| 455 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
| 456 | random_doc, /*tp_doc*/ |
| 457 | 0, /*tp_traverse*/ |
| 458 | 0, /*tp_clear*/ |
| 459 | 0, /*tp_richcompare*/ |
| 460 | 0, /*tp_weaklistoffset*/ |
| 461 | 0, /*tp_iter*/ |
| 462 | 0, /*tp_iternext*/ |
| 463 | random_methods, /*tp_methods*/ |
| 464 | 0, /*tp_members*/ |
| 465 | 0, /*tp_getset*/ |
| 466 | 0, /*tp_base*/ |
| 467 | 0, /*tp_dict*/ |
| 468 | 0, /*tp_descr_get*/ |
| 469 | 0, /*tp_descr_set*/ |
| 470 | 0, /*tp_dictoffset*/ |
| 471 | 0, /*tp_init*/ |
| 472 | 0, /*tp_alloc*/ |
| 473 | random_new, /*tp_new*/ |
| 474 | PyObject_Free, /*tp_free*/ |
| 475 | 0, /*tp_is_gc*/ |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 476 | }; |
| 477 | |
| 478 | PyDoc_STRVAR(module_doc, |
| 479 | "Module implements the Mersenne Twister random number generator."); |
| 480 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 481 | |
| 482 | static struct PyModuleDef _randommodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 483 | PyModuleDef_HEAD_INIT, |
| 484 | "_random", |
| 485 | module_doc, |
| 486 | -1, |
| 487 | NULL, |
| 488 | NULL, |
| 489 | NULL, |
| 490 | NULL, |
| 491 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 492 | }; |
| 493 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 494 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 495 | PyInit__random(void) |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 496 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 497 | PyObject *m; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 498 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 499 | if (PyType_Ready(&Random_Type) < 0) |
| 500 | return NULL; |
| 501 | m = PyModule_Create(&_randommodule); |
| 502 | if (m == NULL) |
| 503 | return NULL; |
| 504 | Py_INCREF(&Random_Type); |
| 505 | PyModule_AddObject(m, "Random", (PyObject *)&Random_Type); |
| 506 | return m; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 507 | } |