blob: 4377ee0cf4d90ce78b1970636236599e0e0be1c3 [file] [log] [blame]
Raymond Hettinger40f62172002-12-29 23:03:38 +00001/* Random objects */
2
3/* ------------------------------------------------------------------
4 The code in this module was based on a download from:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005 http://www.math.keio.ac.jp/~matumoto/MT2002/emt19937ar.html
Raymond Hettinger40f62172002-12-29 23:03:38 +00006
7 It was modified in 2002 by Raymond Hettinger as follows:
8
Benjamin Petersond8e5f2d2010-08-24 18:08:22 +00009 * the principal computational lines untouched.
Raymond Hettinger40f62172002-12-29 23:03:38 +000010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000011 * renamed genrand_res53() to random_random() and wrapped
12 in python calling/return code.
Raymond Hettinger40f62172002-12-29 23:03:38 +000013
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000014 * genrand_int32() and the helper functions, init_genrand()
15 and init_by_array(), were declared static, wrapped in
16 Python calling/return code. also, their global data
17 references were replaced with structure references.
Raymond Hettinger40f62172002-12-29 23:03:38 +000018
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000019 * unused functions from the original were deleted.
20 new, original C python code was added to implement the
21 Random() interface.
Raymond Hettinger40f62172002-12-29 23:03:38 +000022
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 Pitrouf95a1b32010-05-09 15:52:27 +000039 notice, this list of conditions and the following disclaimer.
Raymond Hettinger40f62172002-12-29 23:03:38 +000040
41 2. Redistributions in binary form must reproduce the above copyright
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000042 notice, this list of conditions and the following disclaimer in the
43 documentation and/or other materials provided with the distribution.
Raymond Hettinger40f62172002-12-29 23:03:38 +000044
45 3. The names of its contributors may not be used to endorse or promote
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000046 products derived from this software without specific prior written
47 permission.
Raymond Hettinger40f62172002-12-29 23:03:38 +000048
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.
63 http://www.math.keio.ac.jp/matumoto/emt.html
64 email: matumoto@math.keio.ac.jp
65*/
66
67/* ---------------------------------------------------------------*/
68
69#include "Python.h"
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000070#include <time.h> /* for seeding to current time */
Raymond Hettinger40f62172002-12-29 23:03:38 +000071
72/* Period parameters -- These are all magic. Don't change. */
73#define N 624
74#define M 397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075#define MATRIX_A 0x9908b0dfUL /* constant vector a */
Raymond Hettinger40f62172002-12-29 23:03:38 +000076#define UPPER_MASK 0x80000000UL /* most significant w-r bits */
77#define LOWER_MASK 0x7fffffffUL /* least significant r bits */
78
79typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 PyObject_HEAD
81 unsigned long state[N];
82 int index;
Raymond Hettinger40f62172002-12-29 23:03:38 +000083} RandomObject;
84
85static PyTypeObject Random_Type;
86
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087#define RandomObject_Check(v) (Py_TYPE(v) == &Random_Type)
Raymond Hettinger40f62172002-12-29 23:03:38 +000088
89
90/* Random methods */
91
92
93/* generates a random number on [0,0xffffffff]-interval */
94static unsigned long
95genrand_int32(RandomObject *self)
96{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 unsigned long y;
98 static unsigned long mag01[2]={0x0UL, MATRIX_A};
99 /* mag01[x] = x * MATRIX_A for x=0,1 */
100 unsigned long *mt;
Raymond Hettinger40f62172002-12-29 23:03:38 +0000101
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102 mt = self->state;
103 if (self->index >= N) { /* generate N words at one time */
104 int kk;
Raymond Hettinger40f62172002-12-29 23:03:38 +0000105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 for (kk=0;kk<N-M;kk++) {
107 y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
108 mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL];
109 }
110 for (;kk<N-1;kk++) {
111 y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
112 mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
113 }
114 y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
115 mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];
Raymond Hettinger40f62172002-12-29 23:03:38 +0000116
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 self->index = 0;
118 }
Raymond Hettinger40f62172002-12-29 23:03:38 +0000119
120 y = mt[self->index++];
121 y ^= (y >> 11);
122 y ^= (y << 7) & 0x9d2c5680UL;
123 y ^= (y << 15) & 0xefc60000UL;
124 y ^= (y >> 18);
125 return y;
126}
127
128/* random_random is the function named genrand_res53 in the original code;
129 * generates a random number on [0,1) with 53-bit resolution; note that
130 * 9007199254740992 == 2**53; I assume they're spelling "/2**53" as
131 * multiply-by-reciprocal in the (likely vain) hope that the compiler will
132 * optimize the division away at compile-time. 67108864 is 2**26. In
133 * effect, a contains 27 random bits shifted left 26, and b fills in the
134 * lower 26 bits of the 53-bit numerator.
135 * The orginal code credited Isaku Wada for this algorithm, 2002/01/09.
136 */
137static PyObject *
138random_random(RandomObject *self)
139{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 unsigned long a=genrand_int32(self)>>5, b=genrand_int32(self)>>6;
141 return PyFloat_FromDouble((a*67108864.0+b)*(1.0/9007199254740992.0));
Raymond Hettinger40f62172002-12-29 23:03:38 +0000142}
143
144/* initializes mt[N] with a seed */
145static void
146init_genrand(RandomObject *self, unsigned long s)
147{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 int mti;
149 unsigned long *mt;
Raymond Hettinger40f62172002-12-29 23:03:38 +0000150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 mt = self->state;
152 mt[0]= s & 0xffffffffUL;
153 for (mti=1; mti<N; mti++) {
154 mt[mti] =
155 (1812433253UL * (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 mt[mti] &= 0xffffffffUL;
161 /* for >32 bit machines */
162 }
163 self->index = mti;
164 return;
Raymond Hettinger40f62172002-12-29 23:03:38 +0000165}
166
167/* initialize by an array with array-length */
168/* init_key is the array for initializing keys */
169/* key_length is its length */
170static PyObject *
Mark Dickinson4cd60172012-12-21 21:52:49 +0000171init_by_array(RandomObject *self, unsigned long init_key[], size_t key_length)
Raymond Hettinger40f62172002-12-29 23:03:38 +0000172{
Mark Dickinson4cd60172012-12-21 21:52:49 +0000173 size_t i, j, k; /* was signed in the original code. RDH 12/16/2002 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174 unsigned long *mt;
Raymond Hettinger40f62172002-12-29 23:03:38 +0000175
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 mt = self->state;
177 init_genrand(self, 19650218UL);
178 i=1; j=0;
179 k = (N>key_length ? N : key_length);
180 for (; k; k--) {
181 mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL))
Victor Stinner1109b542013-11-15 23:16:15 +0100182 + init_key[j] + (unsigned long)j; /* non linear */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
184 i++; j++;
185 if (i>=N) { mt[0] = mt[N-1]; i=1; }
186 if (j>=key_length) j=0;
187 }
188 for (k=N-1; k; k--) {
189 mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL))
Victor Stinner1109b542013-11-15 23:16:15 +0100190 - (unsigned long)i; /* non linear */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
192 i++;
193 if (i>=N) { mt[0] = mt[N-1]; i=1; }
194 }
Raymond Hettinger40f62172002-12-29 23:03:38 +0000195
196 mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */
197 Py_INCREF(Py_None);
198 return Py_None;
199}
200
201/*
202 * The rest is Python-specific code, neither part of, nor derived from, the
203 * Twister download.
204 */
205
206static PyObject *
207random_seed(RandomObject *self, PyObject *args)
208{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 PyObject *result = NULL; /* guilty until proved innocent */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 PyObject *n = NULL;
Mark Dickinson4cd60172012-12-21 21:52:49 +0000211 unsigned long *key = NULL;
212 unsigned char *key_as_bytes = NULL;
213 size_t bits, keyused, i;
214 int res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 PyObject *arg = NULL;
Raymond Hettinger40f62172002-12-29 23:03:38 +0000216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 if (!PyArg_UnpackTuple(args, "seed", 0, 1, &arg))
218 return NULL;
Raymond Hettinger40f62172002-12-29 23:03:38 +0000219
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 if (arg == NULL || arg == Py_None) {
221 time_t now;
Raymond Hettinger40f62172002-12-29 23:03:38 +0000222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 time(&now);
224 init_genrand(self, (unsigned long)now);
225 Py_INCREF(Py_None);
226 return Py_None;
227 }
Larry Hastingsd60cd422012-06-24 02:52:21 -0700228 /* This algorithm relies on the number being unsigned.
229 * So: if the arg is a PyLong, use its absolute value.
230 * Otherwise use its hash value, cast to unsigned.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 */
232 if (PyLong_Check(arg))
233 n = PyNumber_Absolute(arg);
234 else {
Larry Hastingsd60cd422012-06-24 02:52:21 -0700235 Py_hash_t hash = PyObject_Hash(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 if (hash == -1)
237 goto Done;
Larry Hastingsd60cd422012-06-24 02:52:21 -0700238 n = PyLong_FromSize_t((size_t)hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 }
240 if (n == NULL)
241 goto Done;
Raymond Hettinger40f62172002-12-29 23:03:38 +0000242
Mark Dickinson4cd60172012-12-21 21:52:49 +0000243 /* Now split n into 32-bit chunks, from the right. */
244 bits = _PyLong_NumBits(n);
245 if (bits == (size_t)-1 && PyErr_Occurred())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 goto Done;
Raymond Hettinger40f62172002-12-29 23:03:38 +0000247
Mark Dickinson4cd60172012-12-21 21:52:49 +0000248 /* Figure out how many 32-bit chunks this gives us. */
249 keyused = bits == 0 ? 1 : (bits - 1) / 32 + 1;
Raymond Hettinger40f62172002-12-29 23:03:38 +0000250
Mark Dickinson4cd60172012-12-21 21:52:49 +0000251 /* Convert seed to byte sequence. */
252 key_as_bytes = (unsigned char *)PyMem_Malloc((size_t)4 * keyused);
Victor Stinnera4ced862013-07-15 20:00:36 +0200253 if (key_as_bytes == NULL) {
254 PyErr_NoMemory();
Mark Dickinson4cd60172012-12-21 21:52:49 +0000255 goto Done;
Victor Stinnera4ced862013-07-15 20:00:36 +0200256 }
Mark Dickinson4cd60172012-12-21 21:52:49 +0000257 res = _PyLong_AsByteArray((PyLongObject *)n,
258 key_as_bytes, keyused * 4,
259 1, /* little-endian */
260 0); /* unsigned */
261 if (res == -1) {
262 PyMem_Free(key_as_bytes);
263 goto Done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 }
Raymond Hettinger40f62172002-12-29 23:03:38 +0000265
Mark Dickinson4cd60172012-12-21 21:52:49 +0000266 /* Fill array of unsigned longs from byte sequence. */
267 key = (unsigned long *)PyMem_Malloc(sizeof(unsigned long) * keyused);
268 if (key == NULL) {
Victor Stinnera4ced862013-07-15 20:00:36 +0200269 PyErr_NoMemory();
Mark Dickinson4cd60172012-12-21 21:52:49 +0000270 PyMem_Free(key_as_bytes);
271 goto Done;
272 }
273 for (i = 0; i < keyused; i++) {
274 key[i] =
275 ((unsigned long)key_as_bytes[4*i + 0] << 0) +
276 ((unsigned long)key_as_bytes[4*i + 1] << 8) +
277 ((unsigned long)key_as_bytes[4*i + 2] << 16) +
278 ((unsigned long)key_as_bytes[4*i + 3] << 24);
279 }
280 PyMem_Free(key_as_bytes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 result = init_by_array(self, key, keyused);
Raymond Hettinger40f62172002-12-29 23:03:38 +0000282Done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 Py_XDECREF(n);
284 PyMem_Free(key);
285 return result;
Raymond Hettinger40f62172002-12-29 23:03:38 +0000286}
287
288static PyObject *
289random_getstate(RandomObject *self)
290{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 PyObject *state;
292 PyObject *element;
293 int i;
Raymond Hettinger40f62172002-12-29 23:03:38 +0000294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 state = PyTuple_New(N+1);
296 if (state == NULL)
297 return NULL;
298 for (i=0; i<N ; i++) {
299 element = PyLong_FromUnsignedLong(self->state[i]);
300 if (element == NULL)
301 goto Fail;
302 PyTuple_SET_ITEM(state, i, element);
303 }
304 element = PyLong_FromLong((long)(self->index));
305 if (element == NULL)
306 goto Fail;
307 PyTuple_SET_ITEM(state, i, element);
308 return state;
Raymond Hettinger40f62172002-12-29 23:03:38 +0000309
310Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 Py_DECREF(state);
312 return NULL;
Raymond Hettinger40f62172002-12-29 23:03:38 +0000313}
314
315static PyObject *
316random_setstate(RandomObject *self, PyObject *state)
317{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 int i;
319 unsigned long element;
320 long index;
Raymond Hettinger40f62172002-12-29 23:03:38 +0000321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 if (!PyTuple_Check(state)) {
323 PyErr_SetString(PyExc_TypeError,
324 "state vector must be a tuple");
325 return NULL;
326 }
327 if (PyTuple_Size(state) != N+1) {
328 PyErr_SetString(PyExc_ValueError,
329 "state vector is the wrong size");
330 return NULL;
331 }
Raymond Hettinger40f62172002-12-29 23:03:38 +0000332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 for (i=0; i<N ; i++) {
334 element = PyLong_AsUnsignedLong(PyTuple_GET_ITEM(state, i));
335 if (element == (unsigned long)-1 && PyErr_Occurred())
336 return NULL;
337 self->state[i] = element & 0xffffffffUL; /* Make sure we get sane state */
338 }
Raymond Hettinger40f62172002-12-29 23:03:38 +0000339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 index = PyLong_AsLong(PyTuple_GET_ITEM(state, i));
341 if (index == -1 && PyErr_Occurred())
342 return NULL;
343 self->index = (int)index;
Raymond Hettinger40f62172002-12-29 23:03:38 +0000344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 Py_INCREF(Py_None);
346 return Py_None;
Raymond Hettinger40f62172002-12-29 23:03:38 +0000347}
348
Raymond Hettinger40f62172002-12-29 23:03:38 +0000349static PyObject *
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000350random_getrandbits(RandomObject *self, PyObject *args)
351{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 int k, i, bytes;
353 unsigned long r;
354 unsigned char *bytearray;
355 PyObject *result;
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 if (!PyArg_ParseTuple(args, "i:getrandbits", &k))
358 return NULL;
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 if (k <= 0) {
361 PyErr_SetString(PyExc_ValueError,
362 "number of bits must be greater than zero");
363 return NULL;
364 }
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000365
Serhiy Storchakad8a0bac2013-01-04 12:18:35 +0200366 if (k <= 32) /* Fast path */
367 return PyLong_FromUnsignedLong(genrand_int32(self) >> (32 - k));
368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 bytes = ((k - 1) / 32 + 1) * 4;
370 bytearray = (unsigned char *)PyMem_Malloc(bytes);
371 if (bytearray == NULL) {
372 PyErr_NoMemory();
373 return NULL;
374 }
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 /* Fill-out whole words, byte-by-byte to avoid endianness issues */
377 for (i=0 ; i<bytes ; i+=4, k-=32) {
378 r = genrand_int32(self);
379 if (k < 32)
380 r >>= (32 - k);
381 bytearray[i+0] = (unsigned char)r;
382 bytearray[i+1] = (unsigned char)(r >> 8);
383 bytearray[i+2] = (unsigned char)(r >> 16);
384 bytearray[i+3] = (unsigned char)(r >> 24);
385 }
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000386
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 /* little endian order to match bytearray assignment order */
388 result = _PyLong_FromByteArray(bytearray, bytes, 1, 0);
389 PyMem_Free(bytearray);
390 return result;
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000391}
392
393static PyObject *
Raymond Hettinger40f62172002-12-29 23:03:38 +0000394random_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
395{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 RandomObject *self;
397 PyObject *tmp;
Raymond Hettinger40f62172002-12-29 23:03:38 +0000398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 if (type == &Random_Type && !_PyArg_NoKeywords("Random()", kwds))
400 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +0000401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 self = (RandomObject *)type->tp_alloc(type, 0);
403 if (self == NULL)
404 return NULL;
405 tmp = random_seed(self, args);
406 if (tmp == NULL) {
407 Py_DECREF(self);
408 return NULL;
409 }
410 Py_DECREF(tmp);
411 return (PyObject *)self;
Raymond Hettinger40f62172002-12-29 23:03:38 +0000412}
413
414static PyMethodDef random_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 {"random", (PyCFunction)random_random, METH_NOARGS,
416 PyDoc_STR("random() -> x in the interval [0, 1).")},
417 {"seed", (PyCFunction)random_seed, METH_VARARGS,
418 PyDoc_STR("seed([n]) -> None. Defaults to current time.")},
419 {"getstate", (PyCFunction)random_getstate, METH_NOARGS,
420 PyDoc_STR("getstate() -> tuple containing the current state.")},
421 {"setstate", (PyCFunction)random_setstate, METH_O,
422 PyDoc_STR("setstate(state) -> None. Restores generator state.")},
423 {"getrandbits", (PyCFunction)random_getrandbits, METH_VARARGS,
Serhiy Storchaka95949422013-08-27 19:40:23 +0300424 PyDoc_STR("getrandbits(k) -> x. Generates an int with "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 "k random bits.")},
426 {NULL, NULL} /* sentinel */
Raymond Hettinger40f62172002-12-29 23:03:38 +0000427};
428
429PyDoc_STRVAR(random_doc,
430"Random() -> create a random number generator with its own internal state.");
431
432static PyTypeObject Random_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 PyVarObject_HEAD_INIT(NULL, 0)
434 "_random.Random", /*tp_name*/
435 sizeof(RandomObject), /*tp_basicsize*/
436 0, /*tp_itemsize*/
437 /* methods */
438 0, /*tp_dealloc*/
439 0, /*tp_print*/
440 0, /*tp_getattr*/
441 0, /*tp_setattr*/
442 0, /*tp_reserved*/
443 0, /*tp_repr*/
444 0, /*tp_as_number*/
445 0, /*tp_as_sequence*/
446 0, /*tp_as_mapping*/
447 0, /*tp_hash*/
448 0, /*tp_call*/
449 0, /*tp_str*/
450 PyObject_GenericGetAttr, /*tp_getattro*/
451 0, /*tp_setattro*/
452 0, /*tp_as_buffer*/
453 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
454 random_doc, /*tp_doc*/
455 0, /*tp_traverse*/
456 0, /*tp_clear*/
457 0, /*tp_richcompare*/
458 0, /*tp_weaklistoffset*/
459 0, /*tp_iter*/
460 0, /*tp_iternext*/
461 random_methods, /*tp_methods*/
462 0, /*tp_members*/
463 0, /*tp_getset*/
464 0, /*tp_base*/
465 0, /*tp_dict*/
466 0, /*tp_descr_get*/
467 0, /*tp_descr_set*/
468 0, /*tp_dictoffset*/
469 0, /*tp_init*/
470 0, /*tp_alloc*/
471 random_new, /*tp_new*/
472 PyObject_Free, /*tp_free*/
473 0, /*tp_is_gc*/
Raymond Hettinger40f62172002-12-29 23:03:38 +0000474};
475
476PyDoc_STRVAR(module_doc,
477"Module implements the Mersenne Twister random number generator.");
478
Martin v. Löwis1a214512008-06-11 05:26:20 +0000479
480static struct PyModuleDef _randommodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 PyModuleDef_HEAD_INIT,
482 "_random",
483 module_doc,
484 -1,
485 NULL,
486 NULL,
487 NULL,
488 NULL,
489 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000490};
491
Raymond Hettinger40f62172002-12-29 23:03:38 +0000492PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000493PyInit__random(void)
Raymond Hettinger40f62172002-12-29 23:03:38 +0000494{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 PyObject *m;
Raymond Hettinger40f62172002-12-29 23:03:38 +0000496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 if (PyType_Ready(&Random_Type) < 0)
498 return NULL;
499 m = PyModule_Create(&_randommodule);
500 if (m == NULL)
501 return NULL;
502 Py_INCREF(&Random_Type);
503 PyModule_AddObject(m, "Random", (PyObject *)&Random_Type);
504 return m;
Raymond Hettinger40f62172002-12-29 23:03:38 +0000505}