| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  | /* Random objects */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ------------------------------------------------------------------
 | 
					
						
							|  |  |  |    The code in this module was based on a download from: | 
					
						
							| 
									
										
										
										
											2017-02-15 18:04:43 +09:00
										 |  |  |       http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/emt19937ar.html
 | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    It was modified in 2002 by Raymond Hettinger as follows: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-24 18:08:22 +00:00
										 |  |  |     * the principal computational lines untouched. | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     * renamed genrand_res53() to random_random() and wrapped | 
					
						
							|  |  |  |       in python calling/return code. | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     * genrand_int32() and the helper functions, init_genrand() | 
					
						
							|  |  |  |       and init_by_array(), were declared static, wrapped in | 
					
						
							|  |  |  |       Python calling/return code.  also, their global data | 
					
						
							|  |  |  |       references were replaced with structure references. | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     * unused functions from the original were deleted. | 
					
						
							|  |  |  |       new, original C python code was added to implement the | 
					
						
							|  |  |  |       Random() interface. | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    The following are the verbatim comments from the original code: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    A C-program for MT19937, with initialization improved 2002/1/26. | 
					
						
							|  |  |  |    Coded by Takuji Nishimura and Makoto Matsumoto. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Before using, initialize the state by using init_genrand(seed) | 
					
						
							|  |  |  |    or init_by_array(init_key, key_length). | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, | 
					
						
							|  |  |  |    All rights reserved. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Redistribution and use in source and binary forms, with or without | 
					
						
							|  |  |  |    modification, are permitted provided that the following conditions | 
					
						
							|  |  |  |    are met: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |      1. Redistributions of source code must retain the above copyright | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     notice, this list of conditions and the following disclaimer. | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |      2. Redistributions in binary form must reproduce the above copyright | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     notice, this list of conditions and the following disclaimer in the | 
					
						
							|  |  |  |     documentation and/or other materials provided with the distribution. | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |      3. The names of its contributors may not be used to endorse or promote | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     products derived from this software without specific prior written | 
					
						
							|  |  |  |     permission. | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
					
						
							|  |  |  |    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
					
						
							|  |  |  |    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 
					
						
							|  |  |  |    A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR | 
					
						
							|  |  |  |    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | 
					
						
							|  |  |  |    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 
					
						
							|  |  |  |    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | 
					
						
							|  |  |  |    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | 
					
						
							|  |  |  |    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | 
					
						
							|  |  |  |    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | 
					
						
							|  |  |  |    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Any feedback is very welcome. | 
					
						
							| 
									
										
										
										
											2017-02-15 18:04:43 +09:00
										 |  |  |    http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
 | 
					
						
							|  |  |  |    email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space) | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ---------------------------------------------------------------*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "Python.h"
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | #include <time.h>               /* for seeding to current time */
 | 
					
						
							| 
									
										
										
										
											2016-09-06 17:03:03 -07:00
										 |  |  | #ifdef HAVE_PROCESS_H
 | 
					
						
							|  |  |  | #  include <process.h>          /* needed for getpid() */
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Period parameters -- These are all magic.  Don't change. */ | 
					
						
							|  |  |  | #define N 624
 | 
					
						
							|  |  |  | #define M 397
 | 
					
						
							| 
									
										
										
										
											2015-05-13 15:02:12 +03:00
										 |  |  | #define MATRIX_A 0x9908b0dfU    /* constant vector a */
 | 
					
						
							|  |  |  | #define UPPER_MASK 0x80000000U  /* most significant w-r bits */
 | 
					
						
							|  |  |  | #define LOWER_MASK 0x7fffffffU  /* least significant r bits */
 | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-13 11:12:27 +01:00
										 |  |  | typedef struct { | 
					
						
							|  |  |  |     PyObject *Random_Type; | 
					
						
							|  |  |  |     PyObject *Long___abs__; | 
					
						
							|  |  |  | } _randomstate; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define _randomstate(o) ((_randomstate *)PyModule_GetState(o))
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct PyModuleDef _randommodule; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define _randomstate_global _randomstate(PyState_FindModule(&_randommodule))
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  | typedef struct { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyObject_HEAD | 
					
						
							|  |  |  |     int index; | 
					
						
							| 
									
										
										
										
											2016-09-06 13:24:00 -07:00
										 |  |  |     uint32_t state[N]; | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  | } RandomObject; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-24 22:09:23 +01:00
										 |  |  | #include "clinic/_randommodule.c.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | module _random | 
					
						
							|  |  |  | class _random.Random "RandomObject *" "&Random_Type" | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=f79898ae7847c321]*/ | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Random methods */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* generates a random number on [0,0xffffffff]-interval */ | 
					
						
							| 
									
										
										
										
											2016-09-06 13:24:00 -07:00
										 |  |  | static uint32_t | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  | genrand_int32(RandomObject *self) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-09-06 13:24:00 -07:00
										 |  |  |     uint32_t y; | 
					
						
							|  |  |  |     static const uint32_t mag01[2] = {0x0U, MATRIX_A}; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     /* mag01[x] = x * MATRIX_A  for x=0,1 */ | 
					
						
							| 
									
										
										
										
											2016-09-06 13:24:00 -07:00
										 |  |  |     uint32_t *mt; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     mt = self->state; | 
					
						
							|  |  |  |     if (self->index >= N) { /* generate N words at one time */ | 
					
						
							|  |  |  |         int kk; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for (kk=0;kk<N-M;kk++) { | 
					
						
							|  |  |  |             y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK); | 
					
						
							| 
									
										
										
										
											2015-05-13 15:02:12 +03:00
										 |  |  |             mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1U]; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |         for (;kk<N-1;kk++) { | 
					
						
							|  |  |  |             y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK); | 
					
						
							| 
									
										
										
										
											2015-05-13 15:02:12 +03:00
										 |  |  |             mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1U]; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |         y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK); | 
					
						
							| 
									
										
										
										
											2015-05-13 15:02:12 +03:00
										 |  |  |         mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1U]; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         self->index = 0; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     y = mt[self->index++]; | 
					
						
							|  |  |  |     y ^= (y >> 11); | 
					
						
							| 
									
										
										
										
											2015-05-13 15:02:12 +03:00
										 |  |  |     y ^= (y << 7) & 0x9d2c5680U; | 
					
						
							|  |  |  |     y ^= (y << 15) & 0xefc60000U; | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  |     y ^= (y >> 18); | 
					
						
							|  |  |  |     return y; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* random_random is the function named genrand_res53 in the original code;
 | 
					
						
							|  |  |  |  * generates a random number on [0,1) with 53-bit resolution; note that | 
					
						
							|  |  |  |  * 9007199254740992 == 2**53; I assume they're spelling "/2**53" as | 
					
						
							|  |  |  |  * multiply-by-reciprocal in the (likely vain) hope that the compiler will | 
					
						
							|  |  |  |  * optimize the division away at compile-time.  67108864 is 2**26.  In | 
					
						
							|  |  |  |  * effect, a contains 27 random bits shifted left 26, and b fills in the | 
					
						
							|  |  |  |  * lower 26 bits of the 53-bit numerator. | 
					
						
							| 
									
										
										
										
											2016-04-29 16:54:10 +03:00
										 |  |  |  * The original code credited Isaku Wada for this algorithm, 2002/01/09. | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-05-24 22:09:23 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | _random.Random.random | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   self: self(type="RandomObject *") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | random() -> x in the interval [0, 1). | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2019-05-24 22:09:23 +01:00
										 |  |  | _random_Random_random_impl(RandomObject *self) | 
					
						
							|  |  |  | /*[clinic end generated code: output=117ff99ee53d755c input=afb2a59cbbb00349]*/ | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-09-06 13:24:00 -07:00
										 |  |  |     uint32_t a=genrand_int32(self)>>5, b=genrand_int32(self)>>6; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     return PyFloat_FromDouble((a*67108864.0+b)*(1.0/9007199254740992.0)); | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* initializes mt[N] with a seed */ | 
					
						
							|  |  |  | static void | 
					
						
							| 
									
										
										
										
											2016-09-06 13:24:00 -07:00
										 |  |  | init_genrand(RandomObject *self, uint32_t s) | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     int mti; | 
					
						
							| 
									
										
										
										
											2016-09-06 13:24:00 -07:00
										 |  |  |     uint32_t *mt; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     mt = self->state; | 
					
						
							| 
									
										
										
										
											2015-05-13 15:02:12 +03:00
										 |  |  |     mt[0]= s; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     for (mti=1; mti<N; mti++) { | 
					
						
							|  |  |  |         mt[mti] = | 
					
						
							| 
									
										
										
										
											2015-05-13 15:02:12 +03:00
										 |  |  |         (1812433253U * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */ | 
					
						
							|  |  |  |         /* In the previous versions, MSBs of the seed affect   */ | 
					
						
							|  |  |  |         /* only MSBs of the array mt[].                                */ | 
					
						
							|  |  |  |         /* 2002/01/09 modified by Makoto Matsumoto                     */ | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     self->index = mti; | 
					
						
							|  |  |  |     return; | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* initialize by an array with array-length */ | 
					
						
							|  |  |  | /* init_key is the array for initializing keys */ | 
					
						
							|  |  |  | /* key_length is its length */ | 
					
						
							| 
									
										
										
										
											2016-09-06 16:33:52 -07:00
										 |  |  | static void | 
					
						
							| 
									
										
										
										
											2016-09-06 13:24:00 -07:00
										 |  |  | init_by_array(RandomObject *self, uint32_t init_key[], size_t key_length) | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2012-12-21 21:52:49 +00:00
										 |  |  |     size_t i, j, k;       /* was signed in the original code. RDH 12/16/2002 */ | 
					
						
							| 
									
										
										
										
											2016-09-06 13:24:00 -07:00
										 |  |  |     uint32_t *mt; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     mt = self->state; | 
					
						
							| 
									
										
										
										
											2015-05-13 15:02:12 +03:00
										 |  |  |     init_genrand(self, 19650218U); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     i=1; j=0; | 
					
						
							|  |  |  |     k = (N>key_length ? N : key_length); | 
					
						
							|  |  |  |     for (; k; k--) { | 
					
						
							| 
									
										
										
										
											2015-05-13 15:02:12 +03:00
										 |  |  |         mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525U)) | 
					
						
							| 
									
										
										
										
											2016-09-06 13:24:00 -07:00
										 |  |  |                  + init_key[j] + (uint32_t)j; /* non linear */ | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         i++; j++; | 
					
						
							|  |  |  |         if (i>=N) { mt[0] = mt[N-1]; i=1; } | 
					
						
							|  |  |  |         if (j>=key_length) j=0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     for (k=N-1; k; k--) { | 
					
						
							| 
									
										
										
										
											2015-05-13 15:02:12 +03:00
										 |  |  |         mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941U)) | 
					
						
							| 
									
										
										
										
											2016-09-06 13:24:00 -07:00
										 |  |  |                  - (uint32_t)i; /* non linear */ | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         i++; | 
					
						
							|  |  |  |         if (i>=N) { mt[0] = mt[N-1]; i=1; } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-13 15:02:12 +03:00
										 |  |  |     mt[0] = 0x80000000U; /* MSB is 1; assuring non-zero initial array */ | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * The rest is Python-specific code, neither part of, nor derived from, the | 
					
						
							|  |  |  |  * Twister download. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-06 16:33:52 -07:00
										 |  |  | static int | 
					
						
							|  |  |  | random_seed_urandom(RandomObject *self) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     PY_UINT32_T key[N]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (_PyOS_URandomNonblock(key, sizeof(key)) < 0) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     init_by_array(self, key, Py_ARRAY_LENGTH(key)); | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | random_seed_time_pid(RandomObject *self) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     _PyTime_t now; | 
					
						
							|  |  |  |     uint32_t key[5]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     now = _PyTime_GetSystemClock(); | 
					
						
							|  |  |  |     key[0] = (PY_UINT32_T)(now & 0xffffffffU); | 
					
						
							|  |  |  |     key[1] = (PY_UINT32_T)(now >> 32); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     key[2] = (PY_UINT32_T)getpid(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     now = _PyTime_GetMonotonicClock(); | 
					
						
							|  |  |  |     key[3] = (PY_UINT32_T)(now & 0xffffffffU); | 
					
						
							|  |  |  |     key[4] = (PY_UINT32_T)(now >> 32); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     init_by_array(self, key, Py_ARRAY_LENGTH(key)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2019-05-24 22:09:23 +01:00
										 |  |  | random_seed(RandomObject *self, PyObject *arg) | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyObject *result = NULL;            /* guilty until proved innocent */ | 
					
						
							|  |  |  |     PyObject *n = NULL; | 
					
						
							| 
									
										
										
										
											2016-09-06 13:24:00 -07:00
										 |  |  |     uint32_t *key = NULL; | 
					
						
							| 
									
										
										
										
											2015-05-13 15:02:12 +03:00
										 |  |  |     size_t bits, keyused; | 
					
						
							| 
									
										
										
										
											2012-12-21 21:52:49 +00:00
										 |  |  |     int res; | 
					
						
							| 
									
										
										
										
											2019-09-13 11:12:27 +01:00
										 |  |  |     PyObject *args[1]; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-24 22:09:23 +01:00
										 |  |  |     if (arg == NULL || arg == Py_None) { | 
					
						
							|  |  |  |        if (random_seed_urandom(self) < 0) { | 
					
						
							| 
									
										
										
										
											2016-09-06 16:33:52 -07:00
										 |  |  |             PyErr_Clear(); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-06 16:33:52 -07:00
										 |  |  |             /* Reading system entropy failed, fall back on the worst entropy:
 | 
					
						
							|  |  |  |                use the current time and process identifier. */ | 
					
						
							|  |  |  |             random_seed_time_pid(self); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         Py_RETURN_NONE; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-06 16:33:52 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-24 02:52:21 -07:00
										 |  |  |     /* This algorithm relies on the number being unsigned.
 | 
					
						
							|  |  |  |      * So: if the arg is a PyLong, use its absolute value. | 
					
						
							|  |  |  |      * Otherwise use its hash value, cast to unsigned. | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-09-13 11:12:27 +01:00
										 |  |  |     if (PyLong_CheckExact(arg)) { | 
					
						
							|  |  |  |         n = PyNumber_Absolute(arg); | 
					
						
							|  |  |  |     } else if (PyLong_Check(arg)) { | 
					
						
							| 
									
										
										
										
											2017-09-28 10:50:01 +03:00
										 |  |  |         /* Calling int.__abs__() prevents calling arg.__abs__(), which might
 | 
					
						
							|  |  |  |            return an invalid value. See issue #31478. */ | 
					
						
							| 
									
										
										
										
											2019-09-13 11:12:27 +01:00
										 |  |  |         args[0] = arg; | 
					
						
							|  |  |  |         n = _PyObject_Vectorcall(_randomstate_global->Long___abs__, args, 0, | 
					
						
							|  |  |  |                                          NULL); | 
					
						
							| 
									
										
										
										
											2017-09-28 10:50:01 +03:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     else { | 
					
						
							| 
									
										
										
										
											2012-06-24 02:52:21 -07:00
										 |  |  |         Py_hash_t hash = PyObject_Hash(arg); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         if (hash == -1) | 
					
						
							|  |  |  |             goto Done; | 
					
						
							| 
									
										
										
										
											2012-06-24 02:52:21 -07:00
										 |  |  |         n = PyLong_FromSize_t((size_t)hash); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |     if (n == NULL) | 
					
						
							|  |  |  |         goto Done; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-12-21 21:52:49 +00:00
										 |  |  |     /* Now split n into 32-bit chunks, from the right. */ | 
					
						
							|  |  |  |     bits = _PyLong_NumBits(n); | 
					
						
							|  |  |  |     if (bits == (size_t)-1 && PyErr_Occurred()) | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         goto Done; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-12-21 21:52:49 +00:00
										 |  |  |     /* Figure out how many 32-bit chunks this gives us. */ | 
					
						
							|  |  |  |     keyused = bits == 0 ? 1 : (bits - 1) / 32 + 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Convert seed to byte sequence. */ | 
					
						
							| 
									
										
										
										
											2016-09-06 13:24:00 -07:00
										 |  |  |     key = (uint32_t *)PyMem_Malloc((size_t)4 * keyused); | 
					
						
							| 
									
										
										
										
											2015-05-13 15:02:12 +03:00
										 |  |  |     if (key == NULL) { | 
					
						
							| 
									
										
										
										
											2013-07-15 20:00:36 +02:00
										 |  |  |         PyErr_NoMemory(); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         goto Done; | 
					
						
							| 
									
										
										
										
											2013-07-15 20:00:36 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2012-12-21 21:52:49 +00:00
										 |  |  |     res = _PyLong_AsByteArray((PyLongObject *)n, | 
					
						
							| 
									
										
										
										
											2015-05-13 15:02:12 +03:00
										 |  |  |                               (unsigned char *)key, keyused * 4, | 
					
						
							|  |  |  |                               PY_LITTLE_ENDIAN, | 
					
						
							| 
									
										
										
										
											2012-12-21 21:52:49 +00:00
										 |  |  |                               0); /* unsigned */ | 
					
						
							|  |  |  |     if (res == -1) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         goto Done; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-13 15:02:12 +03:00
										 |  |  | #if PY_BIG_ENDIAN
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         size_t i, j; | 
					
						
							|  |  |  |         /* Reverse an array. */ | 
					
						
							| 
									
										
										
										
											2015-05-17 23:46:22 -05:00
										 |  |  |         for (i = 0, j = keyused - 1; i < j; i++, j--) { | 
					
						
							| 
									
										
										
										
											2016-09-06 13:24:00 -07:00
										 |  |  |             uint32_t tmp = key[i]; | 
					
						
							| 
									
										
										
										
											2015-05-13 15:02:12 +03:00
										 |  |  |             key[i] = key[j]; | 
					
						
							|  |  |  |             key[j] = tmp; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2012-12-21 21:52:49 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-05-13 15:02:12 +03:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2016-09-06 16:33:52 -07:00
										 |  |  |     init_by_array(self, key, keyused); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Py_INCREF(Py_None); | 
					
						
							|  |  |  |     result = Py_None; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  | Done: | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     Py_XDECREF(n); | 
					
						
							|  |  |  |     PyMem_Free(key); | 
					
						
							|  |  |  |     return result; | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-24 22:09:23 +01:00
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | _random.Random.seed | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   self: self(type="RandomObject *") | 
					
						
							|  |  |  |   n: object = None | 
					
						
							|  |  |  |   / | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | seed([n]) -> None. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Defaults to use urandom and falls back to a combination | 
					
						
							|  |  |  | of the current time and the process identifier. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | _random_Random_seed_impl(RandomObject *self, PyObject *n) | 
					
						
							|  |  |  | /*[clinic end generated code: output=0fad1e16ba883681 input=78d6ef0d52532a54]*/ | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return random_seed(self, n); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | _random.Random.getstate | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   self: self(type="RandomObject *") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | getstate() -> tuple containing the current state. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2019-05-24 22:09:23 +01:00
										 |  |  | _random_Random_getstate_impl(RandomObject *self) | 
					
						
							|  |  |  | /*[clinic end generated code: output=bf6cef0c092c7180 input=b937a487928c0e89]*/ | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyObject *state; | 
					
						
							|  |  |  |     PyObject *element; | 
					
						
							|  |  |  |     int i; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     state = PyTuple_New(N+1); | 
					
						
							|  |  |  |     if (state == NULL) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     for (i=0; i<N ; i++) { | 
					
						
							|  |  |  |         element = PyLong_FromUnsignedLong(self->state[i]); | 
					
						
							|  |  |  |         if (element == NULL) | 
					
						
							|  |  |  |             goto Fail; | 
					
						
							|  |  |  |         PyTuple_SET_ITEM(state, i, element); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     element = PyLong_FromLong((long)(self->index)); | 
					
						
							|  |  |  |     if (element == NULL) | 
					
						
							|  |  |  |         goto Fail; | 
					
						
							|  |  |  |     PyTuple_SET_ITEM(state, i, element); | 
					
						
							|  |  |  |     return state; | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | Fail: | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     Py_DECREF(state); | 
					
						
							|  |  |  |     return NULL; | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-24 22:09:23 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | _random.Random.setstate | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   self: self(type="RandomObject *") | 
					
						
							|  |  |  |   state: object | 
					
						
							|  |  |  |   / | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | setstate(state) -> None.  Restores generator state. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2019-05-24 22:09:23 +01:00
										 |  |  | _random_Random_setstate(RandomObject *self, PyObject *state) | 
					
						
							|  |  |  | /*[clinic end generated code: output=fd1c3cd0037b6681 input=b3b4efbb1bc66af8]*/ | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     int i; | 
					
						
							|  |  |  |     unsigned long element; | 
					
						
							|  |  |  |     long index; | 
					
						
							| 
									
										
										
										
											2017-04-21 23:10:46 -07:00
										 |  |  |     uint32_t new_state[N]; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!PyTuple_Check(state)) { | 
					
						
							|  |  |  |         PyErr_SetString(PyExc_TypeError, | 
					
						
							|  |  |  |             "state vector must be a tuple"); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (PyTuple_Size(state) != N+1) { | 
					
						
							|  |  |  |         PyErr_SetString(PyExc_ValueError, | 
					
						
							|  |  |  |             "state vector is the wrong size"); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (i=0; i<N ; i++) { | 
					
						
							|  |  |  |         element = PyLong_AsUnsignedLong(PyTuple_GET_ITEM(state, i)); | 
					
						
							|  |  |  |         if (element == (unsigned long)-1 && PyErr_Occurred()) | 
					
						
							|  |  |  |             return NULL; | 
					
						
							| 
									
										
										
										
											2017-04-21 23:10:46 -07:00
										 |  |  |         new_state[i] = (uint32_t)element; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     index = PyLong_AsLong(PyTuple_GET_ITEM(state, i)); | 
					
						
							|  |  |  |     if (index == -1 && PyErr_Occurred()) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2015-07-24 09:02:53 +03:00
										 |  |  |     if (index < 0 || index > N) { | 
					
						
							|  |  |  |         PyErr_SetString(PyExc_ValueError, "invalid state"); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     self->index = (int)index; | 
					
						
							| 
									
										
										
										
											2017-04-21 23:10:46 -07:00
										 |  |  |     for (i = 0; i < N; i++) | 
					
						
							|  |  |  |         self->state[i] = new_state[i]; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-23 09:47:21 +02:00
										 |  |  |     Py_RETURN_NONE; | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-24 22:09:23 +01:00
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | _random.Random.getrandbits | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   self: self(type="RandomObject *") | 
					
						
							|  |  |  |   k: int | 
					
						
							|  |  |  |   / | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | getrandbits(k) -> x.  Generates an int with k random bits. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-10-05 09:09:15 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2019-05-24 22:09:23 +01:00
										 |  |  | _random_Random_getrandbits_impl(RandomObject *self, int k) | 
					
						
							|  |  |  | /*[clinic end generated code: output=b402f82a2158887f input=8c0e6396dd176fc0]*/ | 
					
						
							| 
									
										
										
										
											2003-10-05 09:09:15 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-05-24 22:09:23 +01:00
										 |  |  |     int i, words; | 
					
						
							| 
									
										
										
										
											2016-09-06 13:24:00 -07:00
										 |  |  |     uint32_t r; | 
					
						
							|  |  |  |     uint32_t *wordarray; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyObject *result; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (k <= 0) { | 
					
						
							|  |  |  |         PyErr_SetString(PyExc_ValueError, | 
					
						
							|  |  |  |                         "number of bits must be greater than zero"); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-04 12:18:35 +02:00
										 |  |  |     if (k <= 32)  /* Fast path */ | 
					
						
							|  |  |  |         return PyLong_FromUnsignedLong(genrand_int32(self) >> (32 - k)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-13 15:02:12 +03:00
										 |  |  |     words = (k - 1) / 32 + 1; | 
					
						
							| 
									
										
										
										
											2016-09-06 13:24:00 -07:00
										 |  |  |     wordarray = (uint32_t *)PyMem_Malloc(words * 4); | 
					
						
							| 
									
										
										
										
											2015-05-13 15:02:12 +03:00
										 |  |  |     if (wordarray == NULL) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         PyErr_NoMemory(); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-13 15:02:12 +03:00
										 |  |  |     /* Fill-out bits of long integer, by 32-bit words, from least significant
 | 
					
						
							|  |  |  |        to most significant. */ | 
					
						
							|  |  |  | #if PY_LITTLE_ENDIAN
 | 
					
						
							|  |  |  |     for (i = 0; i < words; i++, k -= 32) | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |     for (i = words - 1; i >= 0; i--, k -= 32) | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         r = genrand_int32(self); | 
					
						
							|  |  |  |         if (k < 32) | 
					
						
							| 
									
										
										
										
											2015-05-13 15:02:12 +03:00
										 |  |  |             r >>= (32 - k);  /* Drop least significant bits */ | 
					
						
							|  |  |  |         wordarray[i] = r; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-13 15:02:12 +03:00
										 |  |  |     result = _PyLong_FromByteArray((unsigned char *)wordarray, words * 4, | 
					
						
							|  |  |  |                                    PY_LITTLE_ENDIAN, 0 /* unsigned */); | 
					
						
							|  |  |  |     PyMem_Free(wordarray); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     return result; | 
					
						
							| 
									
										
										
										
											2003-10-05 09:09:15 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  | static PyObject * | 
					
						
							|  |  |  | random_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     RandomObject *self; | 
					
						
							|  |  |  |     PyObject *tmp; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-13 11:12:27 +01:00
										 |  |  |     if (type == (PyTypeObject*)_randomstate_global->Random_Type && | 
					
						
							|  |  |  |         !_PyArg_NoKeywords("Random()", kwds)) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2019-09-13 11:12:27 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-13 11:12:27 +01:00
										 |  |  |     self = (RandomObject *)PyType_GenericAlloc(type, 0); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (self == NULL) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     tmp = random_seed(self, args); | 
					
						
							|  |  |  |     if (tmp == NULL) { | 
					
						
							|  |  |  |         Py_DECREF(self); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     Py_DECREF(tmp); | 
					
						
							|  |  |  |     return (PyObject *)self; | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyMethodDef random_methods[] = { | 
					
						
							| 
									
										
										
										
											2019-05-24 22:09:23 +01:00
										 |  |  |     _RANDOM_RANDOM_RANDOM_METHODDEF | 
					
						
							|  |  |  |     _RANDOM_RANDOM_SEED_METHODDEF | 
					
						
							|  |  |  |     _RANDOM_RANDOM_GETSTATE_METHODDEF | 
					
						
							|  |  |  |     _RANDOM_RANDOM_SETSTATE_METHODDEF | 
					
						
							|  |  |  |     _RANDOM_RANDOM_GETRANDBITS_METHODDEF | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     {NULL,              NULL}           /* sentinel */ | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyDoc_STRVAR(random_doc, | 
					
						
							|  |  |  | "Random() -> create a random number generator with its own internal state."); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-13 11:12:27 +01:00
										 |  |  | static PyType_Slot Random_Type_slots[] = { | 
					
						
							| 
									
										
										
										
											2019-10-01 12:45:52 +02:00
										 |  |  |     {Py_tp_doc, (void *)random_doc}, | 
					
						
							| 
									
										
										
										
											2019-09-13 11:12:27 +01:00
										 |  |  |     {Py_tp_methods, random_methods}, | 
					
						
							|  |  |  |     {Py_tp_new, random_new}, | 
					
						
							|  |  |  |     {Py_tp_free, PyObject_Free}, | 
					
						
							|  |  |  |     {0, 0}, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyType_Spec Random_Type_spec = { | 
					
						
							|  |  |  |     "_random.Random", | 
					
						
							|  |  |  |     sizeof(RandomObject), | 
					
						
							|  |  |  |     0, | 
					
						
							|  |  |  |     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, | 
					
						
							|  |  |  |     Random_Type_slots | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyDoc_STRVAR(module_doc, | 
					
						
							|  |  |  | "Module implements the Mersenne Twister random number generator."); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-13 11:12:27 +01:00
										 |  |  | static int | 
					
						
							|  |  |  | _random_traverse(PyObject *module, visitproc visit, void *arg) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Py_VISIT(_randomstate(module)->Random_Type); | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | _random_clear(PyObject *module) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Py_CLEAR(_randomstate(module)->Random_Type); | 
					
						
							| 
									
										
										
										
											2019-09-15 15:51:44 +01:00
										 |  |  |     Py_CLEAR(_randomstate(module)->Long___abs__); | 
					
						
							| 
									
										
										
										
											2019-09-13 11:12:27 +01:00
										 |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | _random_free(void *module) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     _random_clear((PyObject *)module); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2008-06-11 05:26:20 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static struct PyModuleDef _randommodule = { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyModuleDef_HEAD_INIT, | 
					
						
							|  |  |  |     "_random", | 
					
						
							|  |  |  |     module_doc, | 
					
						
							| 
									
										
										
										
											2019-09-13 11:12:27 +01:00
										 |  |  |     sizeof(_randomstate), | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     NULL, | 
					
						
							|  |  |  |     NULL, | 
					
						
							| 
									
										
										
										
											2019-09-13 11:12:27 +01:00
										 |  |  |     _random_traverse, | 
					
						
							|  |  |  |     _random_clear, | 
					
						
							|  |  |  |     _random_free, | 
					
						
							| 
									
										
										
										
											2008-06-11 05:26:20 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  | PyMODINIT_FUNC | 
					
						
							| 
									
										
										
										
											2008-06-11 05:26:20 +00:00
										 |  |  | PyInit__random(void) | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyObject *m; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-13 11:12:27 +01:00
										 |  |  |     PyObject *Random_Type = PyType_FromSpec(&Random_Type_spec); | 
					
						
							|  |  |  |     if (Random_Type == NULL) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2019-09-13 11:12:27 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     m = PyModule_Create(&_randommodule); | 
					
						
							| 
									
										
										
										
											2019-09-13 11:12:27 +01:00
										 |  |  |     if (m == NULL) { | 
					
						
							|  |  |  |         Py_DECREF(Random_Type); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2019-09-13 11:12:27 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     _randomstate(m)->Random_Type = Random_Type; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Py_INCREF(Random_Type); | 
					
						
							|  |  |  |     PyModule_AddObject(m, "Random", Random_Type); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Look up and save int.__abs__, which is needed in random_seed(). */ | 
					
						
							|  |  |  |     PyObject *longval = NULL, *longtype = NULL; | 
					
						
							|  |  |  |     longval = PyLong_FromLong(0); | 
					
						
							|  |  |  |     if (longval == NULL) goto fail; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     longtype = PyObject_Type(longval); | 
					
						
							|  |  |  |     if (longtype == NULL) goto fail; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     PyObject *abs = PyObject_GetAttrString(longtype, "__abs__"); | 
					
						
							|  |  |  |     if (abs == NULL) goto fail; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Py_DECREF(longtype); | 
					
						
							|  |  |  |     Py_DECREF(longval); | 
					
						
							|  |  |  |     _randomstate(m)->Long___abs__ = abs; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     return m; | 
					
						
							| 
									
										
										
										
											2019-09-13 11:12:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | fail: | 
					
						
							|  |  |  |     Py_XDECREF(longtype); | 
					
						
							|  |  |  |     Py_XDECREF(longval); | 
					
						
							|  |  |  |     Py_DECREF(m); | 
					
						
							|  |  |  |     return NULL; | 
					
						
							| 
									
										
										
										
											2002-12-29 23:03:38 +00:00
										 |  |  | } |