_randommodule.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. /* Random objects */
  2. /* ------------------------------------------------------------------
  3. The code in this module was based on a download from:
  4. http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/emt19937ar.html
  5. It was modified in 2002 by Raymond Hettinger as follows:
  6. * the principal computational lines untouched.
  7. * renamed genrand_res53() to random_random() and wrapped
  8. in python calling/return code.
  9. * genrand_uint32() and the helper functions, init_genrand()
  10. and init_by_array(), were declared static, wrapped in
  11. Python calling/return code. also, their global data
  12. references were replaced with structure references.
  13. * unused functions from the original were deleted.
  14. new, original C python code was added to implement the
  15. Random() interface.
  16. The following are the verbatim comments from the original code:
  17. A C-program for MT19937, with initialization improved 2002/1/26.
  18. Coded by Takuji Nishimura and Makoto Matsumoto.
  19. Before using, initialize the state by using init_genrand(seed)
  20. or init_by_array(init_key, key_length).
  21. Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
  22. All rights reserved.
  23. Redistribution and use in source and binary forms, with or without
  24. modification, are permitted provided that the following conditions
  25. are met:
  26. 1. Redistributions of source code must retain the above copyright
  27. notice, this list of conditions and the following disclaimer.
  28. 2. Redistributions in binary form must reproduce the above copyright
  29. notice, this list of conditions and the following disclaimer in the
  30. documentation and/or other materials provided with the distribution.
  31. 3. The names of its contributors may not be used to endorse or promote
  32. products derived from this software without specific prior written
  33. permission.
  34. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  38. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  39. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  40. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  41. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  42. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  43. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  44. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. Any feedback is very welcome.
  46. http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
  47. email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
  48. */
  49. /* ---------------------------------------------------------------*/
  50. #ifndef Py_BUILD_CORE_BUILTIN
  51. # define Py_BUILD_CORE_MODULE 1
  52. #endif
  53. #include "Python.h"
  54. #include "pycore_moduleobject.h" // _PyModule_GetState()
  55. #include "pycore_runtime.h"
  56. #ifdef HAVE_PROCESS_H
  57. # include <process.h> // getpid()
  58. #endif
  59. #ifdef MS_WINDOWS
  60. # include <windows.h>
  61. #endif
  62. /* Period parameters -- These are all magic. Don't change. */
  63. #define N 624
  64. #define M 397
  65. #define MATRIX_A 0x9908b0dfU /* constant vector a */
  66. #define UPPER_MASK 0x80000000U /* most significant w-r bits */
  67. #define LOWER_MASK 0x7fffffffU /* least significant r bits */
  68. typedef struct {
  69. PyObject *Random_Type;
  70. PyObject *Long___abs__;
  71. } _randomstate;
  72. static inline _randomstate*
  73. get_random_state(PyObject *module)
  74. {
  75. void *state = _PyModule_GetState(module);
  76. assert(state != NULL);
  77. return (_randomstate *)state;
  78. }
  79. static struct PyModuleDef _randommodule;
  80. #define _randomstate_type(type) \
  81. (get_random_state(PyType_GetModuleByDef(type, &_randommodule)))
  82. typedef struct {
  83. PyObject_HEAD
  84. int index;
  85. uint32_t state[N];
  86. } RandomObject;
  87. #include "clinic/_randommodule.c.h"
  88. /*[clinic input]
  89. module _random
  90. class _random.Random "RandomObject *" "_randomstate_type(type)->Random_Type"
  91. [clinic start generated code]*/
  92. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=70a2c99619474983]*/
  93. /* Random methods */
  94. /* generates a random number on [0,0xffffffff]-interval */
  95. static uint32_t
  96. genrand_uint32(RandomObject *self)
  97. {
  98. uint32_t y;
  99. static const uint32_t mag01[2] = {0x0U, MATRIX_A};
  100. /* mag01[x] = x * MATRIX_A for x=0,1 */
  101. uint32_t *mt;
  102. mt = self->state;
  103. if (self->index >= N) { /* generate N words at one time */
  104. int kk;
  105. for (kk=0;kk<N-M;kk++) {
  106. y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
  107. mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1U];
  108. }
  109. for (;kk<N-1;kk++) {
  110. y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
  111. mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1U];
  112. }
  113. y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
  114. mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1U];
  115. self->index = 0;
  116. }
  117. y = mt[self->index++];
  118. y ^= (y >> 11);
  119. y ^= (y << 7) & 0x9d2c5680U;
  120. y ^= (y << 15) & 0xefc60000U;
  121. y ^= (y >> 18);
  122. return y;
  123. }
  124. /* random_random is the function named genrand_res53 in the original code;
  125. * generates a random number on [0,1) with 53-bit resolution; note that
  126. * 9007199254740992 == 2**53; I assume they're spelling "/2**53" as
  127. * multiply-by-reciprocal in the (likely vain) hope that the compiler will
  128. * optimize the division away at compile-time. 67108864 is 2**26. In
  129. * effect, a contains 27 random bits shifted left 26, and b fills in the
  130. * lower 26 bits of the 53-bit numerator.
  131. * The original code credited Isaku Wada for this algorithm, 2002/01/09.
  132. */
  133. /*[clinic input]
  134. _random.Random.random
  135. self: self(type="RandomObject *")
  136. random() -> x in the interval [0, 1).
  137. [clinic start generated code]*/
  138. static PyObject *
  139. _random_Random_random_impl(RandomObject *self)
  140. /*[clinic end generated code: output=117ff99ee53d755c input=afb2a59cbbb00349]*/
  141. {
  142. uint32_t a=genrand_uint32(self)>>5, b=genrand_uint32(self)>>6;
  143. return PyFloat_FromDouble((a*67108864.0+b)*(1.0/9007199254740992.0));
  144. }
  145. /* initializes mt[N] with a seed */
  146. static void
  147. init_genrand(RandomObject *self, uint32_t s)
  148. {
  149. int mti;
  150. uint32_t *mt;
  151. mt = self->state;
  152. mt[0]= s;
  153. for (mti=1; mti<N; mti++) {
  154. mt[mti] =
  155. (1812433253U * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
  156. /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
  157. /* In the previous versions, MSBs of the seed affect */
  158. /* only MSBs of the array mt[]. */
  159. /* 2002/01/09 modified by Makoto Matsumoto */
  160. }
  161. self->index = mti;
  162. return;
  163. }
  164. /* initialize by an array with array-length */
  165. /* init_key is the array for initializing keys */
  166. /* key_length is its length */
  167. static void
  168. init_by_array(RandomObject *self, uint32_t init_key[], size_t key_length)
  169. {
  170. size_t i, j, k; /* was signed in the original code. RDH 12/16/2002 */
  171. uint32_t *mt;
  172. mt = self->state;
  173. init_genrand(self, 19650218U);
  174. i=1; j=0;
  175. k = (N>key_length ? N : key_length);
  176. for (; k; k--) {
  177. mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525U))
  178. + init_key[j] + (uint32_t)j; /* non linear */
  179. i++; j++;
  180. if (i>=N) { mt[0] = mt[N-1]; i=1; }
  181. if (j>=key_length) j=0;
  182. }
  183. for (k=N-1; k; k--) {
  184. mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941U))
  185. - (uint32_t)i; /* non linear */
  186. i++;
  187. if (i>=N) { mt[0] = mt[N-1]; i=1; }
  188. }
  189. mt[0] = 0x80000000U; /* MSB is 1; assuring non-zero initial array */
  190. }
  191. /*
  192. * The rest is Python-specific code, neither part of, nor derived from, the
  193. * Twister download.
  194. */
  195. static int
  196. random_seed_urandom(RandomObject *self)
  197. {
  198. uint32_t key[N];
  199. if (_PyOS_URandomNonblock(key, sizeof(key)) < 0) {
  200. return -1;
  201. }
  202. init_by_array(self, key, Py_ARRAY_LENGTH(key));
  203. return 0;
  204. }
  205. static void
  206. random_seed_time_pid(RandomObject *self)
  207. {
  208. _PyTime_t now;
  209. uint32_t key[5];
  210. now = _PyTime_GetSystemClock();
  211. key[0] = (uint32_t)(now & 0xffffffffU);
  212. key[1] = (uint32_t)(now >> 32);
  213. #if defined(MS_WINDOWS) && !defined(MS_WINDOWS_DESKTOP) && !defined(MS_WINDOWS_SYSTEM)
  214. key[2] = (uint32_t)GetCurrentProcessId();
  215. #elif defined(HAVE_GETPID)
  216. key[2] = (uint32_t)getpid();
  217. #else
  218. key[2] = 0;
  219. #endif
  220. now = _PyTime_GetMonotonicClock();
  221. key[3] = (uint32_t)(now & 0xffffffffU);
  222. key[4] = (uint32_t)(now >> 32);
  223. init_by_array(self, key, Py_ARRAY_LENGTH(key));
  224. }
  225. static int
  226. random_seed(RandomObject *self, PyObject *arg)
  227. {
  228. int result = -1; /* guilty until proved innocent */
  229. PyObject *n = NULL;
  230. uint32_t *key = NULL;
  231. size_t bits, keyused;
  232. int res;
  233. if (arg == NULL || arg == Py_None) {
  234. if (random_seed_urandom(self) < 0) {
  235. PyErr_Clear();
  236. /* Reading system entropy failed, fall back on the worst entropy:
  237. use the current time and process identifier. */
  238. random_seed_time_pid(self);
  239. }
  240. return 0;
  241. }
  242. /* This algorithm relies on the number being unsigned.
  243. * So: if the arg is a PyLong, use its absolute value.
  244. * Otherwise use its hash value, cast to unsigned.
  245. */
  246. if (PyLong_CheckExact(arg)) {
  247. n = PyNumber_Absolute(arg);
  248. } else if (PyLong_Check(arg)) {
  249. /* Calling int.__abs__() prevents calling arg.__abs__(), which might
  250. return an invalid value. See issue #31478. */
  251. _randomstate *state = _randomstate_type(Py_TYPE(self));
  252. n = PyObject_CallOneArg(state->Long___abs__, arg);
  253. }
  254. else {
  255. Py_hash_t hash = PyObject_Hash(arg);
  256. if (hash == -1)
  257. goto Done;
  258. n = PyLong_FromSize_t((size_t)hash);
  259. }
  260. if (n == NULL)
  261. goto Done;
  262. /* Now split n into 32-bit chunks, from the right. */
  263. bits = _PyLong_NumBits(n);
  264. if (bits == (size_t)-1 && PyErr_Occurred())
  265. goto Done;
  266. /* Figure out how many 32-bit chunks this gives us. */
  267. keyused = bits == 0 ? 1 : (bits - 1) / 32 + 1;
  268. /* Convert seed to byte sequence. */
  269. key = (uint32_t *)PyMem_Malloc((size_t)4 * keyused);
  270. if (key == NULL) {
  271. PyErr_NoMemory();
  272. goto Done;
  273. }
  274. res = _PyLong_AsByteArray((PyLongObject *)n,
  275. (unsigned char *)key, keyused * 4,
  276. PY_LITTLE_ENDIAN,
  277. 0); /* unsigned */
  278. if (res == -1) {
  279. goto Done;
  280. }
  281. #if PY_BIG_ENDIAN
  282. {
  283. size_t i, j;
  284. /* Reverse an array. */
  285. for (i = 0, j = keyused - 1; i < j; i++, j--) {
  286. uint32_t tmp = key[i];
  287. key[i] = key[j];
  288. key[j] = tmp;
  289. }
  290. }
  291. #endif
  292. init_by_array(self, key, keyused);
  293. result = 0;
  294. Done:
  295. Py_XDECREF(n);
  296. PyMem_Free(key);
  297. return result;
  298. }
  299. /*[clinic input]
  300. _random.Random.seed
  301. self: self(type="RandomObject *")
  302. n: object = None
  303. /
  304. seed([n]) -> None.
  305. Defaults to use urandom and falls back to a combination
  306. of the current time and the process identifier.
  307. [clinic start generated code]*/
  308. static PyObject *
  309. _random_Random_seed_impl(RandomObject *self, PyObject *n)
  310. /*[clinic end generated code: output=0fad1e16ba883681 input=78d6ef0d52532a54]*/
  311. {
  312. if (random_seed(self, n) < 0) {
  313. return NULL;
  314. }
  315. Py_RETURN_NONE;
  316. }
  317. /*[clinic input]
  318. _random.Random.getstate
  319. self: self(type="RandomObject *")
  320. getstate() -> tuple containing the current state.
  321. [clinic start generated code]*/
  322. static PyObject *
  323. _random_Random_getstate_impl(RandomObject *self)
  324. /*[clinic end generated code: output=bf6cef0c092c7180 input=b937a487928c0e89]*/
  325. {
  326. PyObject *state;
  327. PyObject *element;
  328. int i;
  329. state = PyTuple_New(N+1);
  330. if (state == NULL)
  331. return NULL;
  332. for (i=0; i<N ; i++) {
  333. element = PyLong_FromUnsignedLong(self->state[i]);
  334. if (element == NULL)
  335. goto Fail;
  336. PyTuple_SET_ITEM(state, i, element);
  337. }
  338. element = PyLong_FromLong((long)(self->index));
  339. if (element == NULL)
  340. goto Fail;
  341. PyTuple_SET_ITEM(state, i, element);
  342. return state;
  343. Fail:
  344. Py_DECREF(state);
  345. return NULL;
  346. }
  347. /*[clinic input]
  348. _random.Random.setstate
  349. self: self(type="RandomObject *")
  350. state: object
  351. /
  352. setstate(state) -> None. Restores generator state.
  353. [clinic start generated code]*/
  354. static PyObject *
  355. _random_Random_setstate(RandomObject *self, PyObject *state)
  356. /*[clinic end generated code: output=fd1c3cd0037b6681 input=b3b4efbb1bc66af8]*/
  357. {
  358. int i;
  359. unsigned long element;
  360. long index;
  361. uint32_t new_state[N];
  362. if (!PyTuple_Check(state)) {
  363. PyErr_SetString(PyExc_TypeError,
  364. "state vector must be a tuple");
  365. return NULL;
  366. }
  367. if (PyTuple_Size(state) != N+1) {
  368. PyErr_SetString(PyExc_ValueError,
  369. "state vector is the wrong size");
  370. return NULL;
  371. }
  372. for (i=0; i<N ; i++) {
  373. element = PyLong_AsUnsignedLong(PyTuple_GET_ITEM(state, i));
  374. if (element == (unsigned long)-1 && PyErr_Occurred())
  375. return NULL;
  376. new_state[i] = (uint32_t)element;
  377. }
  378. index = PyLong_AsLong(PyTuple_GET_ITEM(state, i));
  379. if (index == -1 && PyErr_Occurred())
  380. return NULL;
  381. if (index < 0 || index > N) {
  382. PyErr_SetString(PyExc_ValueError, "invalid state");
  383. return NULL;
  384. }
  385. self->index = (int)index;
  386. for (i = 0; i < N; i++)
  387. self->state[i] = new_state[i];
  388. Py_RETURN_NONE;
  389. }
  390. /*[clinic input]
  391. _random.Random.getrandbits
  392. self: self(type="RandomObject *")
  393. k: int
  394. /
  395. getrandbits(k) -> x. Generates an int with k random bits.
  396. [clinic start generated code]*/
  397. static PyObject *
  398. _random_Random_getrandbits_impl(RandomObject *self, int k)
  399. /*[clinic end generated code: output=b402f82a2158887f input=8c0e6396dd176fc0]*/
  400. {
  401. int i, words;
  402. uint32_t r;
  403. uint32_t *wordarray;
  404. PyObject *result;
  405. if (k < 0) {
  406. PyErr_SetString(PyExc_ValueError,
  407. "number of bits must be non-negative");
  408. return NULL;
  409. }
  410. if (k == 0)
  411. return PyLong_FromLong(0);
  412. if (k <= 32) /* Fast path */
  413. return PyLong_FromUnsignedLong(genrand_uint32(self) >> (32 - k));
  414. words = (k - 1) / 32 + 1;
  415. wordarray = (uint32_t *)PyMem_Malloc(words * 4);
  416. if (wordarray == NULL) {
  417. PyErr_NoMemory();
  418. return NULL;
  419. }
  420. /* Fill-out bits of long integer, by 32-bit words, from least significant
  421. to most significant. */
  422. #if PY_LITTLE_ENDIAN
  423. for (i = 0; i < words; i++, k -= 32)
  424. #else
  425. for (i = words - 1; i >= 0; i--, k -= 32)
  426. #endif
  427. {
  428. r = genrand_uint32(self);
  429. if (k < 32)
  430. r >>= (32 - k); /* Drop least significant bits */
  431. wordarray[i] = r;
  432. }
  433. result = _PyLong_FromByteArray((unsigned char *)wordarray, words * 4,
  434. PY_LITTLE_ENDIAN, 0 /* unsigned */);
  435. PyMem_Free(wordarray);
  436. return result;
  437. }
  438. static int
  439. random_init(RandomObject *self, PyObject *args, PyObject *kwds)
  440. {
  441. PyObject *arg = NULL;
  442. _randomstate *state = _randomstate_type(Py_TYPE(self));
  443. if ((Py_IS_TYPE(self, (PyTypeObject *)state->Random_Type) ||
  444. Py_TYPE(self)->tp_init == ((PyTypeObject*)state->Random_Type)->tp_init) &&
  445. !_PyArg_NoKeywords("Random", kwds)) {
  446. return -1;
  447. }
  448. if (PyTuple_GET_SIZE(args) > 1) {
  449. PyErr_SetString(PyExc_TypeError, "Random() requires 0 or 1 argument");
  450. return -1;
  451. }
  452. if (PyTuple_GET_SIZE(args) == 1)
  453. arg = PyTuple_GET_ITEM(args, 0);
  454. return random_seed(self, arg);
  455. }
  456. static PyMethodDef random_methods[] = {
  457. _RANDOM_RANDOM_RANDOM_METHODDEF
  458. _RANDOM_RANDOM_SEED_METHODDEF
  459. _RANDOM_RANDOM_GETSTATE_METHODDEF
  460. _RANDOM_RANDOM_SETSTATE_METHODDEF
  461. _RANDOM_RANDOM_GETRANDBITS_METHODDEF
  462. {NULL, NULL} /* sentinel */
  463. };
  464. PyDoc_STRVAR(random_doc,
  465. "Random() -> create a random number generator with its own internal state.");
  466. static PyType_Slot Random_Type_slots[] = {
  467. {Py_tp_doc, (void *)random_doc},
  468. {Py_tp_methods, random_methods},
  469. {Py_tp_new, PyType_GenericNew},
  470. {Py_tp_init, random_init},
  471. {Py_tp_free, PyObject_Free},
  472. {0, 0},
  473. };
  474. static PyType_Spec Random_Type_spec = {
  475. "_random.Random",
  476. sizeof(RandomObject),
  477. 0,
  478. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
  479. Random_Type_slots
  480. };
  481. PyDoc_STRVAR(module_doc,
  482. "Module implements the Mersenne Twister random number generator.");
  483. static int
  484. _random_exec(PyObject *module)
  485. {
  486. _randomstate *state = get_random_state(module);
  487. state->Random_Type = PyType_FromModuleAndSpec(
  488. module, &Random_Type_spec, NULL);
  489. if (state->Random_Type == NULL) {
  490. return -1;
  491. }
  492. if (PyModule_AddType(module, (PyTypeObject *)state->Random_Type) < 0) {
  493. return -1;
  494. }
  495. /* Look up and save int.__abs__, which is needed in random_seed(). */
  496. PyObject *longval = PyLong_FromLong(0);
  497. if (longval == NULL) {
  498. return -1;
  499. }
  500. PyObject *longtype = PyObject_Type(longval);
  501. Py_DECREF(longval);
  502. if (longtype == NULL) {
  503. return -1;
  504. }
  505. state->Long___abs__ = PyObject_GetAttrString(longtype, "__abs__");
  506. Py_DECREF(longtype);
  507. if (state->Long___abs__ == NULL) {
  508. return -1;
  509. }
  510. return 0;
  511. }
  512. static PyModuleDef_Slot _random_slots[] = {
  513. {Py_mod_exec, _random_exec},
  514. {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
  515. {0, NULL}
  516. };
  517. static int
  518. _random_traverse(PyObject *module, visitproc visit, void *arg)
  519. {
  520. Py_VISIT(get_random_state(module)->Random_Type);
  521. return 0;
  522. }
  523. static int
  524. _random_clear(PyObject *module)
  525. {
  526. Py_CLEAR(get_random_state(module)->Random_Type);
  527. Py_CLEAR(get_random_state(module)->Long___abs__);
  528. return 0;
  529. }
  530. static void
  531. _random_free(void *module)
  532. {
  533. _random_clear((PyObject *)module);
  534. }
  535. static struct PyModuleDef _randommodule = {
  536. PyModuleDef_HEAD_INIT,
  537. "_random",
  538. module_doc,
  539. sizeof(_randomstate),
  540. NULL,
  541. _random_slots,
  542. _random_traverse,
  543. _random_clear,
  544. _random_free,
  545. };
  546. PyMODINIT_FUNC
  547. PyInit__random(void)
  548. {
  549. return PyModuleDef_Init(&_randommodule);
  550. }