6969#include "Python.h"
7070#include <time.h> /* for seeding to current time */
7171
72- #ifndef PY_UINT32_T
73- # error "Failed to find an exact-width 32-bit integer type"
74- #endif
75-
7672/* Period parameters -- These are all magic. Don't change. */
7773#define N 624
7874#define M 397
8379typedef struct {
8480 PyObject_HEAD
8581 int index ;
86- PY_UINT32_T state [N ];
82+ uint32_t state [N ];
8783} RandomObject ;
8884
8985static PyTypeObject Random_Type ;
@@ -95,13 +91,13 @@ static PyTypeObject Random_Type;
9591
9692
9793/* generates a random number on [0,0xffffffff]-interval */
98- static PY_UINT32_T
94+ static uint32_t
9995genrand_int32 (RandomObject * self )
10096{
101- PY_UINT32_T y ;
102- static const PY_UINT32_T mag01 [2 ] = {0x0U , MATRIX_A };
97+ uint32_t y ;
98+ static const uint32_t mag01 [2 ] = {0x0U , MATRIX_A };
10399 /* mag01[x] = x * MATRIX_A for x=0,1 */
104- PY_UINT32_T * mt ;
100+ uint32_t * mt ;
105101
106102 mt = self -> state ;
107103 if (self -> index >= N ) { /* generate N words at one time */
@@ -141,16 +137,16 @@ genrand_int32(RandomObject *self)
141137static PyObject *
142138random_random (RandomObject * self )
143139{
144- PY_UINT32_T a = genrand_int32 (self )>>5 , b = genrand_int32 (self )>>6 ;
140+ uint32_t a = genrand_int32 (self )>>5 , b = genrand_int32 (self )>>6 ;
145141 return PyFloat_FromDouble ((a * 67108864.0 + b )* (1.0 /9007199254740992.0 ));
146142}
147143
148144/* initializes mt[N] with a seed */
149145static void
150- init_genrand (RandomObject * self , PY_UINT32_T s )
146+ init_genrand (RandomObject * self , uint32_t s )
151147{
152148 int mti ;
153- PY_UINT32_T * mt ;
149+ uint32_t * mt ;
154150
155151 mt = self -> state ;
156152 mt [0 ]= s ;
@@ -170,25 +166,25 @@ init_genrand(RandomObject *self, PY_UINT32_T s)
170166/* init_key is the array for initializing keys */
171167/* key_length is its length */
172168static PyObject *
173- init_by_array (RandomObject * self , PY_UINT32_T init_key [], size_t key_length )
169+ init_by_array (RandomObject * self , uint32_t init_key [], size_t key_length )
174170{
175171 size_t i , j , k ; /* was signed in the original code. RDH 12/16/2002 */
176- PY_UINT32_T * mt ;
172+ uint32_t * mt ;
177173
178174 mt = self -> state ;
179175 init_genrand (self , 19650218U );
180176 i = 1 ; j = 0 ;
181177 k = (N > key_length ? N : key_length );
182178 for (; k ; k -- ) {
183179 mt [i ] = (mt [i ] ^ ((mt [i - 1 ] ^ (mt [i - 1 ] >> 30 )) * 1664525U ))
184- + init_key [j ] + (PY_UINT32_T )j ; /* non linear */
180+ + init_key [j ] + (uint32_t )j ; /* non linear */
185181 i ++ ; j ++ ;
186182 if (i >=N ) { mt [0 ] = mt [N - 1 ]; i = 1 ; }
187183 if (j >=key_length ) j = 0 ;
188184 }
189185 for (k = N - 1 ; k ; k -- ) {
190186 mt [i ] = (mt [i ] ^ ((mt [i - 1 ] ^ (mt [i - 1 ] >> 30 )) * 1566083941U ))
191- - (PY_UINT32_T )i ; /* non linear */
187+ - (uint32_t )i ; /* non linear */
192188 i ++ ;
193189 if (i >=N ) { mt [0 ] = mt [N - 1 ]; i = 1 ; }
194190 }
@@ -208,7 +204,7 @@ random_seed(RandomObject *self, PyObject *args)
208204{
209205 PyObject * result = NULL ; /* guilty until proved innocent */
210206 PyObject * n = NULL ;
211- PY_UINT32_T * key = NULL ;
207+ uint32_t * key = NULL ;
212208 size_t bits , keyused ;
213209 int res ;
214210 PyObject * arg = NULL ;
@@ -220,7 +216,7 @@ random_seed(RandomObject *self, PyObject *args)
220216 time_t now ;
221217
222218 time (& now );
223- init_genrand (self , (PY_UINT32_T )now );
219+ init_genrand (self , (uint32_t )now );
224220 Py_INCREF (Py_None );
225221 return Py_None ;
226222 }
@@ -248,7 +244,7 @@ random_seed(RandomObject *self, PyObject *args)
248244 keyused = bits == 0 ? 1 : (bits - 1 ) / 32 + 1 ;
249245
250246 /* Convert seed to byte sequence. */
251- key = (PY_UINT32_T * )PyMem_Malloc ((size_t )4 * keyused );
247+ key = (uint32_t * )PyMem_Malloc ((size_t )4 * keyused );
252248 if (key == NULL ) {
253249 PyErr_NoMemory ();
254250 goto Done ;
@@ -267,7 +263,7 @@ random_seed(RandomObject *self, PyObject *args)
267263 size_t i , j ;
268264 /* Reverse an array. */
269265 for (i = 0 , j = keyused - 1 ; i < j ; i ++ , j -- ) {
270- PY_UINT32_T tmp = key [i ];
266+ uint32_t tmp = key [i ];
271267 key [i ] = key [j ];
272268 key [j ] = tmp ;
273269 }
@@ -329,7 +325,7 @@ random_setstate(RandomObject *self, PyObject *state)
329325 element = PyLong_AsUnsignedLong (PyTuple_GET_ITEM (state , i ));
330326 if (element == (unsigned long )-1 && PyErr_Occurred ())
331327 return NULL ;
332- self -> state [i ] = (PY_UINT32_T )element ;
328+ self -> state [i ] = (uint32_t )element ;
333329 }
334330
335331 index = PyLong_AsLong (PyTuple_GET_ITEM (state , i ));
@@ -349,8 +345,8 @@ static PyObject *
349345random_getrandbits (RandomObject * self , PyObject * args )
350346{
351347 int k , i , words ;
352- PY_UINT32_T r ;
353- PY_UINT32_T * wordarray ;
348+ uint32_t r ;
349+ uint32_t * wordarray ;
354350 PyObject * result ;
355351
356352 if (!PyArg_ParseTuple (args , "i:getrandbits" , & k ))
@@ -366,7 +362,7 @@ random_getrandbits(RandomObject *self, PyObject *args)
366362 return PyLong_FromUnsignedLong (genrand_int32 (self ) >> (32 - k ));
367363
368364 words = (k - 1 ) / 32 + 1 ;
369- wordarray = (PY_UINT32_T * )PyMem_Malloc (words * 4 );
365+ wordarray = (uint32_t * )PyMem_Malloc (words * 4 );
370366 if (wordarray == NULL ) {
371367 PyErr_NoMemory ();
372368 return NULL ;
0 commit comments