blob: 2c0daef71f796d8504f4f51220b62fd115b32ed7 [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:
INADA Naoki0f48ecd2017-02-16 01:00:54 +09005 http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/emt19937ar.html
Raymond Hettinger40f62172002-12-29 23:03:38 +00006
7 It was modified in 2002 by Raymond Hettinger as follows:
8
Benjamin Peterson6fb0fd12010-08-24 18:10:46 +00009 * the principal computational lines untouched.
Raymond Hettinger40f62172002-12-29 23:03:38 +000010
Antoine Pitrouc83ea132010-05-09 14:46:46 +000011 * renamed genrand_res53() to random_random() and wrapped
12 in python calling/return code.
Raymond Hettinger40f62172002-12-29 23:03:38 +000013
Antoine Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +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.
INADA Naoki0f48ecd2017-02-16 01:00:54 +090063 http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
64 email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
Raymond Hettinger40f62172002-12-29 23:03:38 +000065*/
66
67/* ---------------------------------------------------------------*/
68
69#include "Python.h"
Antoine Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +0000148 int mti;
149 unsigned long *mt;
Raymond Hettinger40f62172002-12-29 23:03:38 +0000150
Antoine Pitrouc83ea132010-05-09 14:46:46 +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 *
171init_by_array(RandomObject *self, unsigned long init_key[], unsigned long key_length)
172{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000173 unsigned int i, j, k; /* was signed in the original code. RDH 12/16/2002 */
174 unsigned long *mt;
Raymond Hettinger40f62172002-12-29 23:03:38 +0000175
Antoine Pitrouc83ea132010-05-09 14:46:46 +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))
182 + init_key[j] + j; /* non linear */
183 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))
190 - i; /* non linear */
191 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 Pitrouc83ea132010-05-09 14:46:46 +0000209 PyObject *result = NULL; /* guilty until proved innocent */
210 PyObject *masklower = NULL;
211 PyObject *thirtytwo = NULL;
212 PyObject *n = NULL;
213 unsigned long *key = NULL;
214 unsigned long keymax; /* # of allocated slots in key */
215 unsigned long keyused; /* # of used slots in key */
216 int err;
Raymond Hettinger40f62172002-12-29 23:03:38 +0000217
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000218 PyObject *arg = NULL;
Raymond Hettinger40f62172002-12-29 23:03:38 +0000219
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000220 if (!PyArg_UnpackTuple(args, "seed", 0, 1, &arg))
221 return NULL;
Raymond Hettinger40f62172002-12-29 23:03:38 +0000222
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000223 if (arg == NULL || arg == Py_None) {
224 time_t now;
Raymond Hettinger40f62172002-12-29 23:03:38 +0000225
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000226 time(&now);
227 init_genrand(self, (unsigned long)now);
228 Py_INCREF(Py_None);
229 return Py_None;
230 }
231 /* If the arg is an int or long, use its absolute value; else use
232 * the absolute value of its hash code.
Oren Milman13da1a62017-10-03 00:31:42 +0300233 * Calling int.__abs__() or long.__abs__() prevents calling arg.__abs__(),
234 * which might return an invalid value. See issue #31478.
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000235 */
Oren Milman13da1a62017-10-03 00:31:42 +0300236 if (PyInt_Check(arg)) {
237 n = PyInt_Type.tp_as_number->nb_absolute(arg);
238 }
239 else if (PyLong_Check(arg)) {
240 n = PyLong_Type.tp_as_number->nb_absolute(arg);
241 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000242 else {
243 long hash = PyObject_Hash(arg);
244 if (hash == -1)
245 goto Done;
246 n = PyLong_FromUnsignedLong((unsigned long)hash);
247 }
248 if (n == NULL)
249 goto Done;
Raymond Hettinger40f62172002-12-29 23:03:38 +0000250
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000251 /* Now split n into 32-bit chunks, from the right. Each piece is
252 * stored into key, which has a capacity of keymax chunks, of which
253 * keyused are filled. Alas, the repeated shifting makes this a
254 * quadratic-time algorithm; we'd really like to use
255 * _PyLong_AsByteArray here, but then we'd have to break into the
256 * long representation to figure out how big an array was needed
257 * in advance.
258 */
259 keymax = 8; /* arbitrary; grows later if needed */
260 keyused = 0;
261 key = (unsigned long *)PyMem_Malloc(keymax * sizeof(*key));
262 if (key == NULL)
263 goto Done;
Raymond Hettinger40f62172002-12-29 23:03:38 +0000264
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000265 masklower = PyLong_FromUnsignedLong(0xffffffffU);
266 if (masklower == NULL)
267 goto Done;
268 thirtytwo = PyInt_FromLong(32L);
269 if (thirtytwo == NULL)
270 goto Done;
271 while ((err=PyObject_IsTrue(n))) {
272 PyObject *newn;
273 PyObject *pychunk;
274 unsigned long chunk;
Raymond Hettinger40f62172002-12-29 23:03:38 +0000275
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000276 if (err == -1)
277 goto Done;
278 pychunk = PyNumber_And(n, masklower);
279 if (pychunk == NULL)
280 goto Done;
281 chunk = PyLong_AsUnsignedLong(pychunk);
282 Py_DECREF(pychunk);
283 if (chunk == (unsigned long)-1 && PyErr_Occurred())
284 goto Done;
285 newn = PyNumber_Rshift(n, thirtytwo);
286 if (newn == NULL)
287 goto Done;
288 Py_DECREF(n);
289 n = newn;
290 if (keyused >= keymax) {
291 unsigned long bigger = keymax << 1;
292 if ((bigger >> 1) != keymax) {
293 PyErr_NoMemory();
294 goto Done;
295 }
296 key = (unsigned long *)PyMem_Realloc(key,
297 bigger * sizeof(*key));
298 if (key == NULL)
299 goto Done;
300 keymax = bigger;
301 }
302 assert(keyused < keymax);
303 key[keyused++] = chunk;
304 }
Raymond Hettinger40f62172002-12-29 23:03:38 +0000305
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000306 if (keyused == 0)
307 key[keyused++] = 0UL;
308 result = init_by_array(self, key, keyused);
Raymond Hettinger40f62172002-12-29 23:03:38 +0000309Done:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000310 Py_XDECREF(masklower);
311 Py_XDECREF(thirtytwo);
312 Py_XDECREF(n);
313 PyMem_Free(key);
314 return result;
Raymond Hettinger40f62172002-12-29 23:03:38 +0000315}
316
317static PyObject *
318random_getstate(RandomObject *self)
319{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000320 PyObject *state;
321 PyObject *element;
322 int i;
Raymond Hettinger40f62172002-12-29 23:03:38 +0000323
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000324 state = PyTuple_New(N+1);
325 if (state == NULL)
326 return NULL;
327 for (i=0; i<N ; i++) {
328 element = PyLong_FromUnsignedLong(self->state[i]);
329 if (element == NULL)
330 goto Fail;
331 PyTuple_SET_ITEM(state, i, element);
332 }
333 element = PyLong_FromLong((long)(self->index));
334 if (element == NULL)
335 goto Fail;
336 PyTuple_SET_ITEM(state, i, element);
337 return state;
Raymond Hettinger40f62172002-12-29 23:03:38 +0000338
339Fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000340 Py_DECREF(state);
341 return NULL;
Raymond Hettinger40f62172002-12-29 23:03:38 +0000342}
343
344static PyObject *
345random_setstate(RandomObject *self, PyObject *state)
346{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000347 int i;
348 unsigned long element;
349 long index;
Mariatta1626a472017-05-27 07:19:55 -0700350 unsigned long new_state[N];
Raymond Hettinger40f62172002-12-29 23:03:38 +0000351
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000352 if (!PyTuple_Check(state)) {
353 PyErr_SetString(PyExc_TypeError,
354 "state vector must be a tuple");
355 return NULL;
356 }
357 if (PyTuple_Size(state) != N+1) {
358 PyErr_SetString(PyExc_ValueError,
359 "state vector is the wrong size");
360 return NULL;
361 }
Raymond Hettinger40f62172002-12-29 23:03:38 +0000362
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000363 for (i=0; i<N ; i++) {
364 element = PyLong_AsUnsignedLong(PyTuple_GET_ITEM(state, i));
365 if (element == (unsigned long)-1 && PyErr_Occurred())
366 return NULL;
Mariatta1626a472017-05-27 07:19:55 -0700367 new_state[i] = element & 0xffffffffUL; /* Make sure we get sane state */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000368 }
Raymond Hettinger40f62172002-12-29 23:03:38 +0000369
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000370 index = PyLong_AsLong(PyTuple_GET_ITEM(state, i));
371 if (index == -1 && PyErr_Occurred())
372 return NULL;
Serhiy Storchaka3b77d012015-07-24 09:02:53 +0300373 if (index < 0 || index > N) {
374 PyErr_SetString(PyExc_ValueError, "invalid state");
375 return NULL;
376 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000377 self->index = (int)index;
Mariatta1626a472017-05-27 07:19:55 -0700378 for (i = 0; i < N; i++)
379 self->state[i] = new_state[i];
Raymond Hettinger40f62172002-12-29 23:03:38 +0000380
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000381 Py_INCREF(Py_None);
382 return Py_None;
Raymond Hettinger40f62172002-12-29 23:03:38 +0000383}
384
385/*
386Jumpahead should be a fast way advance the generator n-steps ahead, but
387lacking a formula for that, the next best is to use n and the existing
388state to create a new state far away from the original.
389
390The generator uses constant spaced additive feedback, so shuffling the
391state elements ought to produce a state which would not be encountered
392(in the near term) by calls to random(). Shuffling is normally
393implemented by swapping the ith element with another element ranging
394from 0 to i inclusive. That allows the element to have the possibility
395of not being moved. Since the goal is to produce a new, different
396state, the swap element is ranged from 0 to i-1 inclusive. This assures
397that each element gets moved at least once.
398
399To make sure that consecutive calls to jumpahead(n) produce different
400states (even in the rare case of involutory shuffles), i+1 is added to
401each element at position i. Successive calls are then guaranteed to
402have changing (growing) values as well as shuffled positions.
403
404Finally, the self->index value is set to N so that the generator itself
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000405kicks in on the next call to random(). This assures that all results
Raymond Hettinger40f62172002-12-29 23:03:38 +0000406have been through the generator and do not just reflect alterations to
407the underlying state.
408*/
409
410static PyObject *
411random_jumpahead(RandomObject *self, PyObject *n)
412{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000413 long i, j;
414 PyObject *iobj;
415 PyObject *remobj;
Mark Dickinsone0afb722012-06-30 17:19:35 +0100416 unsigned long *mt, tmp, nonzero;
Raymond Hettinger40f62172002-12-29 23:03:38 +0000417
Serhiy Storchaka48c8bf22018-07-31 09:09:36 +0300418 if (!_PyAnyInt_Check(n)) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000419 PyErr_Format(PyExc_TypeError, "jumpahead requires an "
420 "integer, not '%s'",
421 Py_TYPE(n)->tp_name);
422 return NULL;
423 }
Raymond Hettinger40f62172002-12-29 23:03:38 +0000424
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000425 mt = self->state;
426 for (i = N-1; i > 1; i--) {
427 iobj = PyInt_FromLong(i);
428 if (iobj == NULL)
429 return NULL;
430 remobj = PyNumber_Remainder(n, iobj);
431 Py_DECREF(iobj);
432 if (remobj == NULL)
433 return NULL;
434 j = PyInt_AsLong(remobj);
435 Py_DECREF(remobj);
436 if (j == -1L && PyErr_Occurred())
437 return NULL;
438 tmp = mt[i];
439 mt[i] = mt[j];
440 mt[j] = tmp;
441 }
Raymond Hettinger40f62172002-12-29 23:03:38 +0000442
Mark Dickinsone0afb722012-06-30 17:19:35 +0100443 nonzero = 0;
444 for (i = 1; i < N; i++) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000445 mt[i] += i+1;
Mark Dickinsone0afb722012-06-30 17:19:35 +0100446 mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
447 nonzero |= mt[i];
448 }
449
450 /* Ensure the state is nonzero: in the unlikely event that mt[1] through
451 mt[N-1] are all zero, set the MSB of mt[0] (see issue #14591). In the
452 normal case, we fall back to the pre-issue 14591 behaviour for mt[0]. */
453 if (nonzero) {
454 mt[0] += 1;
455 mt[0] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
456 }
457 else {
458 mt[0] = 0x80000000UL;
459 }
Raymond Hettinger40f62172002-12-29 23:03:38 +0000460
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000461 self->index = N;
462 Py_INCREF(Py_None);
463 return Py_None;
Raymond Hettinger40f62172002-12-29 23:03:38 +0000464}
465
466static PyObject *
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000467random_getrandbits(RandomObject *self, PyObject *args)
468{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000469 int k, i, bytes;
470 unsigned long r;
471 unsigned char *bytearray;
472 PyObject *result;
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000473
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000474 if (!PyArg_ParseTuple(args, "i:getrandbits", &k))
475 return NULL;
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000476
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000477 if (k <= 0) {
478 PyErr_SetString(PyExc_ValueError,
479 "number of bits must be greater than zero");
480 return NULL;
481 }
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000482
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000483 bytes = ((k - 1) / 32 + 1) * 4;
484 bytearray = (unsigned char *)PyMem_Malloc(bytes);
485 if (bytearray == NULL) {
486 PyErr_NoMemory();
487 return NULL;
488 }
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000489
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000490 /* Fill-out whole words, byte-by-byte to avoid endianness issues */
491 for (i=0 ; i<bytes ; i+=4, k-=32) {
492 r = genrand_int32(self);
493 if (k < 32)
494 r >>= (32 - k);
495 bytearray[i+0] = (unsigned char)r;
496 bytearray[i+1] = (unsigned char)(r >> 8);
497 bytearray[i+2] = (unsigned char)(r >> 16);
498 bytearray[i+3] = (unsigned char)(r >> 24);
499 }
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000500
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000501 /* little endian order to match bytearray assignment order */
502 result = _PyLong_FromByteArray(bytearray, bytes, 1, 0);
503 PyMem_Free(bytearray);
504 return result;
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000505}
506
507static PyObject *
Raymond Hettinger40f62172002-12-29 23:03:38 +0000508random_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
509{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000510 RandomObject *self;
511 PyObject *tmp;
Raymond Hettinger40f62172002-12-29 23:03:38 +0000512
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000513 if (type == &Random_Type && !_PyArg_NoKeywords("Random()", kwds))
514 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +0000515
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000516 self = (RandomObject *)type->tp_alloc(type, 0);
517 if (self == NULL)
518 return NULL;
519 tmp = random_seed(self, args);
520 if (tmp == NULL) {
521 Py_DECREF(self);
522 return NULL;
523 }
524 Py_DECREF(tmp);
525 return (PyObject *)self;
Raymond Hettinger40f62172002-12-29 23:03:38 +0000526}
527
528static PyMethodDef random_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000529 {"random", (PyCFunction)random_random, METH_NOARGS,
530 PyDoc_STR("random() -> x in the interval [0, 1).")},
531 {"seed", (PyCFunction)random_seed, METH_VARARGS,
532 PyDoc_STR("seed([n]) -> None. Defaults to current time.")},
533 {"getstate", (PyCFunction)random_getstate, METH_NOARGS,
534 PyDoc_STR("getstate() -> tuple containing the current state.")},
535 {"setstate", (PyCFunction)random_setstate, METH_O,
536 PyDoc_STR("setstate(state) -> None. Restores generator state.")},
537 {"jumpahead", (PyCFunction)random_jumpahead, METH_O,
538 PyDoc_STR("jumpahead(int) -> None. Create new state from "
539 "existing state and integer.")},
540 {"getrandbits", (PyCFunction)random_getrandbits, METH_VARARGS,
541 PyDoc_STR("getrandbits(k) -> x. Generates a long int with "
542 "k random bits.")},
543 {NULL, NULL} /* sentinel */
Raymond Hettinger40f62172002-12-29 23:03:38 +0000544};
545
546PyDoc_STRVAR(random_doc,
547"Random() -> create a random number generator with its own internal state.");
548
549static PyTypeObject Random_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000550 PyVarObject_HEAD_INIT(NULL, 0)
551 "_random.Random", /*tp_name*/
552 sizeof(RandomObject), /*tp_basicsize*/
553 0, /*tp_itemsize*/
554 /* methods */
555 0, /*tp_dealloc*/
556 0, /*tp_print*/
557 0, /*tp_getattr*/
558 0, /*tp_setattr*/
559 0, /*tp_compare*/
560 0, /*tp_repr*/
561 0, /*tp_as_number*/
562 0, /*tp_as_sequence*/
563 0, /*tp_as_mapping*/
564 0, /*tp_hash*/
565 0, /*tp_call*/
566 0, /*tp_str*/
567 PyObject_GenericGetAttr, /*tp_getattro*/
568 0, /*tp_setattro*/
569 0, /*tp_as_buffer*/
570 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
571 random_doc, /*tp_doc*/
572 0, /*tp_traverse*/
573 0, /*tp_clear*/
574 0, /*tp_richcompare*/
575 0, /*tp_weaklistoffset*/
576 0, /*tp_iter*/
577 0, /*tp_iternext*/
578 random_methods, /*tp_methods*/
579 0, /*tp_members*/
580 0, /*tp_getset*/
581 0, /*tp_base*/
582 0, /*tp_dict*/
583 0, /*tp_descr_get*/
584 0, /*tp_descr_set*/
585 0, /*tp_dictoffset*/
586 0, /*tp_init*/
587 0, /*tp_alloc*/
588 random_new, /*tp_new*/
589 _PyObject_Del, /*tp_free*/
590 0, /*tp_is_gc*/
Raymond Hettinger40f62172002-12-29 23:03:38 +0000591};
592
593PyDoc_STRVAR(module_doc,
594"Module implements the Mersenne Twister random number generator.");
595
596PyMODINIT_FUNC
597init_random(void)
598{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000599 PyObject *m;
Raymond Hettinger40f62172002-12-29 23:03:38 +0000600
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000601 if (PyType_Ready(&Random_Type) < 0)
602 return;
603 m = Py_InitModule3("_random", NULL, module_doc);
604 if (m == NULL)
605 return;
606 Py_INCREF(&Random_Type);
607 PyModule_AddObject(m, "Random", (PyObject *)&Random_Type);
Raymond Hettinger40f62172002-12-29 23:03:38 +0000608}