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