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 | |
Victor Stinner | 9f5fe79 | 2020-04-17 19:05:35 +0200 | [diff] [blame] | 14 | * genrand_uint32() and the helper functions, init_genrand() |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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" |
Victor Stinner | 9f2a920 | 2016-09-06 17:03:03 -0700 | [diff] [blame] | 70 | #ifdef HAVE_PROCESS_H |
Victor Stinner | 9f5fe79 | 2020-04-17 19:05:35 +0200 | [diff] [blame] | 71 | # include <process.h> // getpid() |
Victor Stinner | 9f2a920 | 2016-09-06 17:03:03 -0700 | [diff] [blame] | 72 | #endif |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 73 | |
| 74 | /* Period parameters -- These are all magic. Don't change. */ |
| 75 | #define N 624 |
| 76 | #define M 397 |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 77 | #define MATRIX_A 0x9908b0dfU /* constant vector a */ |
| 78 | #define UPPER_MASK 0x80000000U /* most significant w-r bits */ |
| 79 | #define LOWER_MASK 0x7fffffffU /* least significant r bits */ |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 80 | |
| 81 | typedef struct { |
Dino Viehland | 04f0bbf | 2019-09-13 11:12:27 +0100 | [diff] [blame] | 82 | PyObject *Random_Type; |
| 83 | PyObject *Long___abs__; |
| 84 | } _randomstate; |
| 85 | |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 86 | static inline _randomstate* |
| 87 | get_random_state(PyObject *module) |
| 88 | { |
| 89 | void *state = PyModule_GetState(module); |
| 90 | assert(state != NULL); |
| 91 | return (_randomstate *)state; |
| 92 | } |
Dino Viehland | 04f0bbf | 2019-09-13 11:12:27 +0100 | [diff] [blame] | 93 | |
| 94 | static struct PyModuleDef _randommodule; |
| 95 | |
Christian Heimes | cc0cd43 | 2020-11-19 08:46:29 +0100 | [diff] [blame] | 96 | #define _randomstate_type(type) \ |
| 97 | (get_random_state(_PyType_GetModuleByDef(type, &_randommodule))) |
Dino Viehland | 04f0bbf | 2019-09-13 11:12:27 +0100 | [diff] [blame] | 98 | |
| 99 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 100 | PyObject_HEAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 101 | int index; |
Benjamin Peterson | 9b3d770 | 2016-09-06 13:24:00 -0700 | [diff] [blame] | 102 | uint32_t state[N]; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 103 | } RandomObject; |
| 104 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 105 | |
Pablo Galindo | 561612d | 2019-05-24 22:09:23 +0100 | [diff] [blame] | 106 | #include "clinic/_randommodule.c.h" |
| 107 | |
| 108 | /*[clinic input] |
| 109 | module _random |
Christian Heimes | cc0cd43 | 2020-11-19 08:46:29 +0100 | [diff] [blame] | 110 | class _random.Random "RandomObject *" "_randomstate_type(type)->Random_Type" |
Pablo Galindo | 561612d | 2019-05-24 22:09:23 +0100 | [diff] [blame] | 111 | [clinic start generated code]*/ |
Christian Heimes | cc0cd43 | 2020-11-19 08:46:29 +0100 | [diff] [blame] | 112 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=70a2c99619474983]*/ |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 113 | |
| 114 | /* Random methods */ |
| 115 | |
| 116 | |
| 117 | /* generates a random number on [0,0xffffffff]-interval */ |
Benjamin Peterson | 9b3d770 | 2016-09-06 13:24:00 -0700 | [diff] [blame] | 118 | static uint32_t |
Victor Stinner | 9f5fe79 | 2020-04-17 19:05:35 +0200 | [diff] [blame] | 119 | genrand_uint32(RandomObject *self) |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 120 | { |
Benjamin Peterson | 9b3d770 | 2016-09-06 13:24:00 -0700 | [diff] [blame] | 121 | uint32_t y; |
| 122 | static const uint32_t mag01[2] = {0x0U, MATRIX_A}; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 123 | /* mag01[x] = x * MATRIX_A for x=0,1 */ |
Benjamin Peterson | 9b3d770 | 2016-09-06 13:24:00 -0700 | [diff] [blame] | 124 | uint32_t *mt; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 125 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 126 | mt = self->state; |
| 127 | if (self->index >= N) { /* generate N words at one time */ |
| 128 | int kk; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 129 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 130 | for (kk=0;kk<N-M;kk++) { |
| 131 | y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK); |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 132 | mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1U]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 133 | } |
| 134 | for (;kk<N-1;kk++) { |
| 135 | y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK); |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 136 | mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1U]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 137 | } |
| 138 | y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK); |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 139 | mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1U]; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 140 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 141 | self->index = 0; |
| 142 | } |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 143 | |
| 144 | y = mt[self->index++]; |
| 145 | y ^= (y >> 11); |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 146 | y ^= (y << 7) & 0x9d2c5680U; |
| 147 | y ^= (y << 15) & 0xefc60000U; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 148 | y ^= (y >> 18); |
| 149 | return y; |
| 150 | } |
| 151 | |
| 152 | /* random_random is the function named genrand_res53 in the original code; |
| 153 | * generates a random number on [0,1) with 53-bit resolution; note that |
| 154 | * 9007199254740992 == 2**53; I assume they're spelling "/2**53" as |
| 155 | * multiply-by-reciprocal in the (likely vain) hope that the compiler will |
| 156 | * optimize the division away at compile-time. 67108864 is 2**26. In |
| 157 | * effect, a contains 27 random bits shifted left 26, and b fills in the |
| 158 | * lower 26 bits of the 53-bit numerator. |
Berker Peksag | 0ac70c0 | 2016-04-29 16:54:10 +0300 | [diff] [blame] | 159 | * The original code credited Isaku Wada for this algorithm, 2002/01/09. |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 160 | */ |
Pablo Galindo | 561612d | 2019-05-24 22:09:23 +0100 | [diff] [blame] | 161 | |
| 162 | /*[clinic input] |
| 163 | _random.Random.random |
| 164 | |
| 165 | self: self(type="RandomObject *") |
| 166 | |
| 167 | random() -> x in the interval [0, 1). |
| 168 | [clinic start generated code]*/ |
| 169 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 170 | static PyObject * |
Pablo Galindo | 561612d | 2019-05-24 22:09:23 +0100 | [diff] [blame] | 171 | _random_Random_random_impl(RandomObject *self) |
| 172 | /*[clinic end generated code: output=117ff99ee53d755c input=afb2a59cbbb00349]*/ |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 173 | { |
Victor Stinner | 9f5fe79 | 2020-04-17 19:05:35 +0200 | [diff] [blame] | 174 | uint32_t a=genrand_uint32(self)>>5, b=genrand_uint32(self)>>6; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 175 | return PyFloat_FromDouble((a*67108864.0+b)*(1.0/9007199254740992.0)); |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /* initializes mt[N] with a seed */ |
| 179 | static void |
Benjamin Peterson | 9b3d770 | 2016-09-06 13:24:00 -0700 | [diff] [blame] | 180 | init_genrand(RandomObject *self, uint32_t s) |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 181 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 182 | int mti; |
Benjamin Peterson | 9b3d770 | 2016-09-06 13:24:00 -0700 | [diff] [blame] | 183 | uint32_t *mt; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 184 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 185 | mt = self->state; |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 186 | mt[0]= s; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 187 | for (mti=1; mti<N; mti++) { |
| 188 | mt[mti] = |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 189 | (1812433253U * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 190 | /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */ |
| 191 | /* In the previous versions, MSBs of the seed affect */ |
| 192 | /* only MSBs of the array mt[]. */ |
| 193 | /* 2002/01/09 modified by Makoto Matsumoto */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 194 | } |
| 195 | self->index = mti; |
| 196 | return; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /* initialize by an array with array-length */ |
| 200 | /* init_key is the array for initializing keys */ |
| 201 | /* key_length is its length */ |
Victor Stinner | e66987e | 2016-09-06 16:33:52 -0700 | [diff] [blame] | 202 | static void |
Benjamin Peterson | 9b3d770 | 2016-09-06 13:24:00 -0700 | [diff] [blame] | 203 | 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] | 204 | { |
Mark Dickinson | 4cd6017 | 2012-12-21 21:52:49 +0000 | [diff] [blame] | 205 | 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] | 206 | uint32_t *mt; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 207 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 208 | mt = self->state; |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 209 | init_genrand(self, 19650218U); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 210 | i=1; j=0; |
| 211 | k = (N>key_length ? N : key_length); |
| 212 | for (; k; k--) { |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 213 | mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525U)) |
Benjamin Peterson | 9b3d770 | 2016-09-06 13:24:00 -0700 | [diff] [blame] | 214 | + init_key[j] + (uint32_t)j; /* non linear */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 215 | i++; j++; |
| 216 | if (i>=N) { mt[0] = mt[N-1]; i=1; } |
| 217 | if (j>=key_length) j=0; |
| 218 | } |
| 219 | for (k=N-1; k; k--) { |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 220 | mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941U)) |
Benjamin Peterson | 9b3d770 | 2016-09-06 13:24:00 -0700 | [diff] [blame] | 221 | - (uint32_t)i; /* non linear */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 222 | i++; |
| 223 | if (i>=N) { mt[0] = mt[N-1]; i=1; } |
| 224 | } |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 225 | |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 226 | mt[0] = 0x80000000U; /* MSB is 1; assuring non-zero initial array */ |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /* |
| 230 | * The rest is Python-specific code, neither part of, nor derived from, the |
| 231 | * Twister download. |
| 232 | */ |
| 233 | |
Victor Stinner | e66987e | 2016-09-06 16:33:52 -0700 | [diff] [blame] | 234 | static int |
| 235 | random_seed_urandom(RandomObject *self) |
| 236 | { |
Victor Stinner | 1a1bd2e | 2020-04-17 19:13:06 +0200 | [diff] [blame] | 237 | uint32_t key[N]; |
Victor Stinner | e66987e | 2016-09-06 16:33:52 -0700 | [diff] [blame] | 238 | |
| 239 | if (_PyOS_URandomNonblock(key, sizeof(key)) < 0) { |
| 240 | return -1; |
| 241 | } |
| 242 | init_by_array(self, key, Py_ARRAY_LENGTH(key)); |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | static void |
| 247 | random_seed_time_pid(RandomObject *self) |
| 248 | { |
| 249 | _PyTime_t now; |
| 250 | uint32_t key[5]; |
| 251 | |
| 252 | now = _PyTime_GetSystemClock(); |
Victor Stinner | 1a1bd2e | 2020-04-17 19:13:06 +0200 | [diff] [blame] | 253 | key[0] = (uint32_t)(now & 0xffffffffU); |
| 254 | key[1] = (uint32_t)(now >> 32); |
Victor Stinner | e66987e | 2016-09-06 16:33:52 -0700 | [diff] [blame] | 255 | |
Victor Stinner | 1a1bd2e | 2020-04-17 19:13:06 +0200 | [diff] [blame] | 256 | key[2] = (uint32_t)getpid(); |
Victor Stinner | e66987e | 2016-09-06 16:33:52 -0700 | [diff] [blame] | 257 | |
| 258 | now = _PyTime_GetMonotonicClock(); |
Victor Stinner | 1a1bd2e | 2020-04-17 19:13:06 +0200 | [diff] [blame] | 259 | key[3] = (uint32_t)(now & 0xffffffffU); |
| 260 | key[4] = (uint32_t)(now >> 32); |
Victor Stinner | e66987e | 2016-09-06 16:33:52 -0700 | [diff] [blame] | 261 | |
| 262 | init_by_array(self, key, Py_ARRAY_LENGTH(key)); |
| 263 | } |
| 264 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 265 | static PyObject * |
Pablo Galindo | 561612d | 2019-05-24 22:09:23 +0100 | [diff] [blame] | 266 | random_seed(RandomObject *self, PyObject *arg) |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 267 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 268 | PyObject *result = NULL; /* guilty until proved innocent */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 269 | PyObject *n = NULL; |
Benjamin Peterson | 9b3d770 | 2016-09-06 13:24:00 -0700 | [diff] [blame] | 270 | uint32_t *key = NULL; |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 271 | size_t bits, keyused; |
Mark Dickinson | 4cd6017 | 2012-12-21 21:52:49 +0000 | [diff] [blame] | 272 | int res; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 273 | |
Pablo Galindo | 561612d | 2019-05-24 22:09:23 +0100 | [diff] [blame] | 274 | if (arg == NULL || arg == Py_None) { |
| 275 | if (random_seed_urandom(self) < 0) { |
Victor Stinner | e66987e | 2016-09-06 16:33:52 -0700 | [diff] [blame] | 276 | PyErr_Clear(); |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 277 | |
Victor Stinner | e66987e | 2016-09-06 16:33:52 -0700 | [diff] [blame] | 278 | /* Reading system entropy failed, fall back on the worst entropy: |
| 279 | use the current time and process identifier. */ |
| 280 | random_seed_time_pid(self); |
| 281 | } |
| 282 | Py_RETURN_NONE; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 283 | } |
Victor Stinner | e66987e | 2016-09-06 16:33:52 -0700 | [diff] [blame] | 284 | |
Larry Hastings | d60cd42 | 2012-06-24 02:52:21 -0700 | [diff] [blame] | 285 | /* This algorithm relies on the number being unsigned. |
| 286 | * So: if the arg is a PyLong, use its absolute value. |
| 287 | * Otherwise use its hash value, cast to unsigned. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 288 | */ |
Dino Viehland | 04f0bbf | 2019-09-13 11:12:27 +0100 | [diff] [blame] | 289 | if (PyLong_CheckExact(arg)) { |
| 290 | n = PyNumber_Absolute(arg); |
| 291 | } else if (PyLong_Check(arg)) { |
Oren Milman | d780b2d | 2017-09-28 10:50:01 +0300 | [diff] [blame] | 292 | /* Calling int.__abs__() prevents calling arg.__abs__(), which might |
| 293 | return an invalid value. See issue #31478. */ |
Christian Heimes | cc0cd43 | 2020-11-19 08:46:29 +0100 | [diff] [blame] | 294 | _randomstate *state = _randomstate_type(Py_TYPE(self)); |
| 295 | n = PyObject_CallOneArg(state->Long___abs__, arg); |
Oren Milman | d780b2d | 2017-09-28 10:50:01 +0300 | [diff] [blame] | 296 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 297 | else { |
Larry Hastings | d60cd42 | 2012-06-24 02:52:21 -0700 | [diff] [blame] | 298 | Py_hash_t hash = PyObject_Hash(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 299 | if (hash == -1) |
| 300 | goto Done; |
Larry Hastings | d60cd42 | 2012-06-24 02:52:21 -0700 | [diff] [blame] | 301 | n = PyLong_FromSize_t((size_t)hash); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 302 | } |
| 303 | if (n == NULL) |
| 304 | goto Done; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 305 | |
Mark Dickinson | 4cd6017 | 2012-12-21 21:52:49 +0000 | [diff] [blame] | 306 | /* Now split n into 32-bit chunks, from the right. */ |
| 307 | bits = _PyLong_NumBits(n); |
| 308 | if (bits == (size_t)-1 && PyErr_Occurred()) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 309 | goto Done; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 310 | |
Mark Dickinson | 4cd6017 | 2012-12-21 21:52:49 +0000 | [diff] [blame] | 311 | /* Figure out how many 32-bit chunks this gives us. */ |
| 312 | keyused = bits == 0 ? 1 : (bits - 1) / 32 + 1; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 313 | |
Mark Dickinson | 4cd6017 | 2012-12-21 21:52:49 +0000 | [diff] [blame] | 314 | /* Convert seed to byte sequence. */ |
Benjamin Peterson | 9b3d770 | 2016-09-06 13:24:00 -0700 | [diff] [blame] | 315 | key = (uint32_t *)PyMem_Malloc((size_t)4 * keyused); |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 316 | if (key == NULL) { |
Victor Stinner | a4ced86 | 2013-07-15 20:00:36 +0200 | [diff] [blame] | 317 | PyErr_NoMemory(); |
Mark Dickinson | 4cd6017 | 2012-12-21 21:52:49 +0000 | [diff] [blame] | 318 | goto Done; |
Victor Stinner | a4ced86 | 2013-07-15 20:00:36 +0200 | [diff] [blame] | 319 | } |
Mark Dickinson | 4cd6017 | 2012-12-21 21:52:49 +0000 | [diff] [blame] | 320 | res = _PyLong_AsByteArray((PyLongObject *)n, |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 321 | (unsigned char *)key, keyused * 4, |
| 322 | PY_LITTLE_ENDIAN, |
Mark Dickinson | 4cd6017 | 2012-12-21 21:52:49 +0000 | [diff] [blame] | 323 | 0); /* unsigned */ |
| 324 | if (res == -1) { |
Mark Dickinson | 4cd6017 | 2012-12-21 21:52:49 +0000 | [diff] [blame] | 325 | goto Done; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 326 | } |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 327 | |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 328 | #if PY_BIG_ENDIAN |
| 329 | { |
| 330 | size_t i, j; |
| 331 | /* Reverse an array. */ |
Zachary Ware | c15ea4c | 2015-05-17 23:46:22 -0500 | [diff] [blame] | 332 | for (i = 0, j = keyused - 1; i < j; i++, j--) { |
Benjamin Peterson | 9b3d770 | 2016-09-06 13:24:00 -0700 | [diff] [blame] | 333 | uint32_t tmp = key[i]; |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 334 | key[i] = key[j]; |
| 335 | key[j] = tmp; |
| 336 | } |
Mark Dickinson | 4cd6017 | 2012-12-21 21:52:49 +0000 | [diff] [blame] | 337 | } |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 338 | #endif |
Victor Stinner | e66987e | 2016-09-06 16:33:52 -0700 | [diff] [blame] | 339 | init_by_array(self, key, keyused); |
| 340 | |
| 341 | Py_INCREF(Py_None); |
| 342 | result = Py_None; |
| 343 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 344 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 345 | Py_XDECREF(n); |
| 346 | PyMem_Free(key); |
| 347 | return result; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Pablo Galindo | 561612d | 2019-05-24 22:09:23 +0100 | [diff] [blame] | 350 | /*[clinic input] |
| 351 | _random.Random.seed |
| 352 | |
| 353 | self: self(type="RandomObject *") |
| 354 | n: object = None |
| 355 | / |
| 356 | |
| 357 | seed([n]) -> None. |
| 358 | |
| 359 | Defaults to use urandom and falls back to a combination |
| 360 | of the current time and the process identifier. |
| 361 | [clinic start generated code]*/ |
| 362 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 363 | static PyObject * |
Pablo Galindo | 561612d | 2019-05-24 22:09:23 +0100 | [diff] [blame] | 364 | _random_Random_seed_impl(RandomObject *self, PyObject *n) |
| 365 | /*[clinic end generated code: output=0fad1e16ba883681 input=78d6ef0d52532a54]*/ |
| 366 | { |
| 367 | return random_seed(self, n); |
| 368 | } |
| 369 | |
| 370 | /*[clinic input] |
| 371 | _random.Random.getstate |
| 372 | |
| 373 | self: self(type="RandomObject *") |
| 374 | |
| 375 | getstate() -> tuple containing the current state. |
| 376 | [clinic start generated code]*/ |
| 377 | |
| 378 | static PyObject * |
| 379 | _random_Random_getstate_impl(RandomObject *self) |
| 380 | /*[clinic end generated code: output=bf6cef0c092c7180 input=b937a487928c0e89]*/ |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 381 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 382 | PyObject *state; |
| 383 | PyObject *element; |
| 384 | int i; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 385 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 386 | state = PyTuple_New(N+1); |
| 387 | if (state == NULL) |
| 388 | return NULL; |
| 389 | for (i=0; i<N ; i++) { |
| 390 | element = PyLong_FromUnsignedLong(self->state[i]); |
| 391 | if (element == NULL) |
| 392 | goto Fail; |
| 393 | PyTuple_SET_ITEM(state, i, element); |
| 394 | } |
| 395 | element = PyLong_FromLong((long)(self->index)); |
| 396 | if (element == NULL) |
| 397 | goto Fail; |
| 398 | PyTuple_SET_ITEM(state, i, element); |
| 399 | return state; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 400 | |
| 401 | Fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 402 | Py_DECREF(state); |
| 403 | return NULL; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Pablo Galindo | 561612d | 2019-05-24 22:09:23 +0100 | [diff] [blame] | 406 | |
| 407 | /*[clinic input] |
| 408 | _random.Random.setstate |
| 409 | |
| 410 | self: self(type="RandomObject *") |
| 411 | state: object |
| 412 | / |
| 413 | |
| 414 | setstate(state) -> None. Restores generator state. |
| 415 | [clinic start generated code]*/ |
| 416 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 417 | static PyObject * |
Pablo Galindo | 561612d | 2019-05-24 22:09:23 +0100 | [diff] [blame] | 418 | _random_Random_setstate(RandomObject *self, PyObject *state) |
| 419 | /*[clinic end generated code: output=fd1c3cd0037b6681 input=b3b4efbb1bc66af8]*/ |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 420 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 421 | int i; |
| 422 | unsigned long element; |
| 423 | long index; |
bladebryan | 9616a82 | 2017-04-21 23:10:46 -0700 | [diff] [blame] | 424 | uint32_t new_state[N]; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 425 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 426 | if (!PyTuple_Check(state)) { |
| 427 | PyErr_SetString(PyExc_TypeError, |
| 428 | "state vector must be a tuple"); |
| 429 | return NULL; |
| 430 | } |
| 431 | if (PyTuple_Size(state) != N+1) { |
| 432 | PyErr_SetString(PyExc_ValueError, |
| 433 | "state vector is the wrong size"); |
| 434 | return NULL; |
| 435 | } |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 436 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 437 | for (i=0; i<N ; i++) { |
| 438 | element = PyLong_AsUnsignedLong(PyTuple_GET_ITEM(state, i)); |
| 439 | if (element == (unsigned long)-1 && PyErr_Occurred()) |
| 440 | return NULL; |
bladebryan | 9616a82 | 2017-04-21 23:10:46 -0700 | [diff] [blame] | 441 | new_state[i] = (uint32_t)element; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 442 | } |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 443 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 444 | index = PyLong_AsLong(PyTuple_GET_ITEM(state, i)); |
| 445 | if (index == -1 && PyErr_Occurred()) |
| 446 | return NULL; |
Serhiy Storchaka | 178f0b6 | 2015-07-24 09:02:53 +0300 | [diff] [blame] | 447 | if (index < 0 || index > N) { |
| 448 | PyErr_SetString(PyExc_ValueError, "invalid state"); |
| 449 | return NULL; |
| 450 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 451 | self->index = (int)index; |
bladebryan | 9616a82 | 2017-04-21 23:10:46 -0700 | [diff] [blame] | 452 | for (i = 0; i < N; i++) |
| 453 | self->state[i] = new_state[i]; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 454 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 455 | Py_RETURN_NONE; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Pablo Galindo | 561612d | 2019-05-24 22:09:23 +0100 | [diff] [blame] | 458 | /*[clinic input] |
| 459 | |
| 460 | _random.Random.getrandbits |
| 461 | |
| 462 | self: self(type="RandomObject *") |
| 463 | k: int |
| 464 | / |
| 465 | |
| 466 | getrandbits(k) -> x. Generates an int with k random bits. |
| 467 | [clinic start generated code]*/ |
| 468 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 469 | static PyObject * |
Pablo Galindo | 561612d | 2019-05-24 22:09:23 +0100 | [diff] [blame] | 470 | _random_Random_getrandbits_impl(RandomObject *self, int k) |
| 471 | /*[clinic end generated code: output=b402f82a2158887f input=8c0e6396dd176fc0]*/ |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 472 | { |
Pablo Galindo | 561612d | 2019-05-24 22:09:23 +0100 | [diff] [blame] | 473 | int i, words; |
Benjamin Peterson | 9b3d770 | 2016-09-06 13:24:00 -0700 | [diff] [blame] | 474 | uint32_t r; |
| 475 | uint32_t *wordarray; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 476 | PyObject *result; |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 477 | |
Antoine Pitrou | 75a3378 | 2020-04-17 19:32:14 +0200 | [diff] [blame] | 478 | if (k < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 479 | PyErr_SetString(PyExc_ValueError, |
Antoine Pitrou | 75a3378 | 2020-04-17 19:32:14 +0200 | [diff] [blame] | 480 | "number of bits must be non-negative"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 481 | return NULL; |
| 482 | } |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 483 | |
Antoine Pitrou | 75a3378 | 2020-04-17 19:32:14 +0200 | [diff] [blame] | 484 | if (k == 0) |
| 485 | return PyLong_FromLong(0); |
| 486 | |
Serhiy Storchaka | d8a0bac | 2013-01-04 12:18:35 +0200 | [diff] [blame] | 487 | if (k <= 32) /* Fast path */ |
Victor Stinner | 9f5fe79 | 2020-04-17 19:05:35 +0200 | [diff] [blame] | 488 | return PyLong_FromUnsignedLong(genrand_uint32(self) >> (32 - k)); |
Serhiy Storchaka | d8a0bac | 2013-01-04 12:18:35 +0200 | [diff] [blame] | 489 | |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 490 | words = (k - 1) / 32 + 1; |
Benjamin Peterson | 9b3d770 | 2016-09-06 13:24:00 -0700 | [diff] [blame] | 491 | wordarray = (uint32_t *)PyMem_Malloc(words * 4); |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 492 | if (wordarray == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 493 | PyErr_NoMemory(); |
| 494 | return NULL; |
| 495 | } |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 496 | |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 497 | /* Fill-out bits of long integer, by 32-bit words, from least significant |
| 498 | to most significant. */ |
| 499 | #if PY_LITTLE_ENDIAN |
| 500 | for (i = 0; i < words; i++, k -= 32) |
| 501 | #else |
| 502 | for (i = words - 1; i >= 0; i--, k -= 32) |
| 503 | #endif |
| 504 | { |
Victor Stinner | 9f5fe79 | 2020-04-17 19:05:35 +0200 | [diff] [blame] | 505 | r = genrand_uint32(self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 506 | if (k < 32) |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 507 | r >>= (32 - k); /* Drop least significant bits */ |
| 508 | wordarray[i] = r; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 509 | } |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 510 | |
Serhiy Storchaka | dce0405 | 2015-05-13 15:02:12 +0300 | [diff] [blame] | 511 | result = _PyLong_FromByteArray((unsigned char *)wordarray, words * 4, |
| 512 | PY_LITTLE_ENDIAN, 0 /* unsigned */); |
| 513 | PyMem_Free(wordarray); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 514 | return result; |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | static PyObject * |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 518 | random_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 519 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 520 | RandomObject *self; |
| 521 | PyObject *tmp; |
Christian Heimes | cc0cd43 | 2020-11-19 08:46:29 +0100 | [diff] [blame] | 522 | _randomstate *state = _randomstate_type(type); |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 523 | |
Christian Heimes | cc0cd43 | 2020-11-19 08:46:29 +0100 | [diff] [blame] | 524 | if (type == (PyTypeObject*)state->Random_Type && |
Dino Viehland | 04f0bbf | 2019-09-13 11:12:27 +0100 | [diff] [blame] | 525 | !_PyArg_NoKeywords("Random()", kwds)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 526 | return NULL; |
Dino Viehland | 04f0bbf | 2019-09-13 11:12:27 +0100 | [diff] [blame] | 527 | } |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 528 | |
Dino Viehland | 04f0bbf | 2019-09-13 11:12:27 +0100 | [diff] [blame] | 529 | self = (RandomObject *)PyType_GenericAlloc(type, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 530 | if (self == NULL) |
| 531 | return NULL; |
| 532 | tmp = random_seed(self, args); |
| 533 | if (tmp == NULL) { |
| 534 | Py_DECREF(self); |
| 535 | return NULL; |
| 536 | } |
| 537 | Py_DECREF(tmp); |
| 538 | return (PyObject *)self; |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Dong-hee Na | 6989af0 | 2020-06-21 18:44:58 +0900 | [diff] [blame] | 541 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 542 | static PyMethodDef random_methods[] = { |
Pablo Galindo | 561612d | 2019-05-24 22:09:23 +0100 | [diff] [blame] | 543 | _RANDOM_RANDOM_RANDOM_METHODDEF |
| 544 | _RANDOM_RANDOM_SEED_METHODDEF |
| 545 | _RANDOM_RANDOM_GETSTATE_METHODDEF |
| 546 | _RANDOM_RANDOM_SETSTATE_METHODDEF |
| 547 | _RANDOM_RANDOM_GETRANDBITS_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 548 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 549 | }; |
| 550 | |
| 551 | PyDoc_STRVAR(random_doc, |
| 552 | "Random() -> create a random number generator with its own internal state."); |
| 553 | |
Dino Viehland | 04f0bbf | 2019-09-13 11:12:27 +0100 | [diff] [blame] | 554 | static PyType_Slot Random_Type_slots[] = { |
Victor Stinner | 2f90261 | 2019-10-01 12:45:52 +0200 | [diff] [blame] | 555 | {Py_tp_doc, (void *)random_doc}, |
Dino Viehland | 04f0bbf | 2019-09-13 11:12:27 +0100 | [diff] [blame] | 556 | {Py_tp_methods, random_methods}, |
| 557 | {Py_tp_new, random_new}, |
| 558 | {Py_tp_free, PyObject_Free}, |
| 559 | {0, 0}, |
| 560 | }; |
| 561 | |
| 562 | static PyType_Spec Random_Type_spec = { |
| 563 | "_random.Random", |
| 564 | sizeof(RandomObject), |
| 565 | 0, |
| 566 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 567 | Random_Type_slots |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 568 | }; |
| 569 | |
| 570 | PyDoc_STRVAR(module_doc, |
| 571 | "Module implements the Mersenne Twister random number generator."); |
| 572 | |
Dino Viehland | 04f0bbf | 2019-09-13 11:12:27 +0100 | [diff] [blame] | 573 | static int |
Christian Heimes | cc0cd43 | 2020-11-19 08:46:29 +0100 | [diff] [blame] | 574 | _random_exec(PyObject *module) |
| 575 | { |
| 576 | _randomstate *state = get_random_state(module); |
| 577 | |
| 578 | state->Random_Type = PyType_FromModuleAndSpec( |
| 579 | module, &Random_Type_spec, NULL); |
| 580 | if (state->Random_Type == NULL) { |
| 581 | return -1; |
| 582 | } |
| 583 | if (PyModule_AddType(module, (PyTypeObject *)state->Random_Type) < 0) { |
| 584 | return -1; |
| 585 | } |
| 586 | |
| 587 | /* Look up and save int.__abs__, which is needed in random_seed(). */ |
| 588 | PyObject *longval = longval = PyLong_FromLong(0); |
| 589 | if (longval == NULL) { |
| 590 | return -1; |
| 591 | } |
| 592 | |
| 593 | PyObject *longtype = PyObject_Type(longval); |
| 594 | Py_DECREF(longval); |
| 595 | if (longtype == NULL) { |
| 596 | return -1; |
| 597 | } |
| 598 | |
| 599 | state->Long___abs__ = PyObject_GetAttrString(longtype, "__abs__"); |
| 600 | Py_DECREF(longtype); |
| 601 | if (state->Long___abs__ == NULL) { |
| 602 | return -1; |
| 603 | } |
| 604 | return 0; |
| 605 | } |
| 606 | |
| 607 | static PyModuleDef_Slot _random_slots[] = { |
| 608 | {Py_mod_exec, _random_exec}, |
| 609 | {0, NULL} |
| 610 | }; |
| 611 | |
| 612 | static int |
Dino Viehland | 04f0bbf | 2019-09-13 11:12:27 +0100 | [diff] [blame] | 613 | _random_traverse(PyObject *module, visitproc visit, void *arg) |
| 614 | { |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 615 | Py_VISIT(get_random_state(module)->Random_Type); |
Dino Viehland | 04f0bbf | 2019-09-13 11:12:27 +0100 | [diff] [blame] | 616 | return 0; |
| 617 | } |
| 618 | |
| 619 | static int |
| 620 | _random_clear(PyObject *module) |
| 621 | { |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 622 | Py_CLEAR(get_random_state(module)->Random_Type); |
| 623 | Py_CLEAR(get_random_state(module)->Long___abs__); |
Dino Viehland | 04f0bbf | 2019-09-13 11:12:27 +0100 | [diff] [blame] | 624 | return 0; |
| 625 | } |
| 626 | |
| 627 | static void |
| 628 | _random_free(void *module) |
| 629 | { |
| 630 | _random_clear((PyObject *)module); |
| 631 | } |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 632 | |
| 633 | static struct PyModuleDef _randommodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 634 | PyModuleDef_HEAD_INIT, |
| 635 | "_random", |
| 636 | module_doc, |
Dino Viehland | 04f0bbf | 2019-09-13 11:12:27 +0100 | [diff] [blame] | 637 | sizeof(_randomstate), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 638 | NULL, |
Christian Heimes | cc0cd43 | 2020-11-19 08:46:29 +0100 | [diff] [blame] | 639 | _random_slots, |
Dino Viehland | 04f0bbf | 2019-09-13 11:12:27 +0100 | [diff] [blame] | 640 | _random_traverse, |
| 641 | _random_clear, |
| 642 | _random_free, |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 643 | }; |
| 644 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 645 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 646 | PyInit__random(void) |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 647 | { |
Christian Heimes | cc0cd43 | 2020-11-19 08:46:29 +0100 | [diff] [blame] | 648 | return PyModuleDef_Init(&_randommodule); |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 649 | } |