2002-12-29 19:03:38 -04:00
|
|
|
/* Random objects */
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------
|
|
|
|
The code in this module was based on a download from:
|
|
|
|
http://www.math.keio.ac.jp/~matumoto/MT2002/emt19937ar.html
|
|
|
|
|
|
|
|
It was modified in 2002 by Raymond Hettinger as follows:
|
|
|
|
|
|
|
|
* the principal computational lines untouched except for tabbing.
|
|
|
|
|
|
|
|
* renamed genrand_res53() to random_random() and wrapped
|
|
|
|
in python calling/return code.
|
|
|
|
|
|
|
|
* 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.
|
|
|
|
|
|
|
|
* unused functions from the original were deleted.
|
|
|
|
new, original C python code was added to implement the
|
|
|
|
Random() interface.
|
|
|
|
|
|
|
|
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
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
3. The names of its contributors may not be used to endorse or promote
|
|
|
|
products derived from this software without specific prior written
|
|
|
|
permission.
|
|
|
|
|
|
|
|
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.
|
|
|
|
http://www.math.keio.ac.jp/matumoto/emt.html
|
|
|
|
email: matumoto@math.keio.ac.jp
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------*/
|
|
|
|
|
|
|
|
#include "Python.h"
|
2003-04-16 15:40:04 -03:00
|
|
|
#include <time.h> /* for seeding to current time */
|
2002-12-29 19:03:38 -04:00
|
|
|
|
|
|
|
/* Period parameters -- These are all magic. Don't change. */
|
|
|
|
#define N 624
|
|
|
|
#define M 397
|
|
|
|
#define MATRIX_A 0x9908b0dfUL /* constant vector a */
|
|
|
|
#define UPPER_MASK 0x80000000UL /* most significant w-r bits */
|
|
|
|
#define LOWER_MASK 0x7fffffffUL /* least significant r bits */
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
PyObject_HEAD
|
|
|
|
unsigned long state[N];
|
|
|
|
int index;
|
|
|
|
} RandomObject;
|
|
|
|
|
|
|
|
static PyTypeObject Random_Type;
|
|
|
|
|
Merged revisions 56467-56482 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/p3yk
................
r56477 | martin.v.loewis | 2007-07-21 09:04:38 +0200 (Sa, 21 Jul 2007) | 11 lines
Merged revisions 56466-56476 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r56476 | martin.v.loewis | 2007-07-21 08:55:02 +0200 (Sa, 21 Jul 2007) | 4 lines
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
backwards compatibility. Add Py_Refcnt, Py_Type, Py_Size, and
PyVarObject_HEAD_INIT.
........
................
r56478 | martin.v.loewis | 2007-07-21 09:47:23 +0200 (Sa, 21 Jul 2007) | 2 lines
PEP 3123: Use proper C inheritance for PyObject.
................
r56479 | martin.v.loewis | 2007-07-21 10:06:55 +0200 (Sa, 21 Jul 2007) | 3 lines
Add longintrepr.h to Python.h, so that the compiler can
see that PyFalse is really some kind of PyObject*.
................
r56480 | martin.v.loewis | 2007-07-21 10:47:18 +0200 (Sa, 21 Jul 2007) | 2 lines
Qualify SHIFT, MASK, BASE.
................
r56482 | martin.v.loewis | 2007-07-21 19:10:57 +0200 (Sa, 21 Jul 2007) | 2 lines
Correctly refer to _ob_next.
................
2007-07-21 14:22:18 -03:00
|
|
|
#define RandomObject_Check(v) (Py_Type(v) == &Random_Type)
|
2002-12-29 19:03:38 -04:00
|
|
|
|
|
|
|
|
|
|
|
/* Random methods */
|
|
|
|
|
|
|
|
|
|
|
|
/* generates a random number on [0,0xffffffff]-interval */
|
|
|
|
static unsigned long
|
|
|
|
genrand_int32(RandomObject *self)
|
|
|
|
{
|
|
|
|
unsigned long y;
|
|
|
|
static unsigned long mag01[2]={0x0UL, MATRIX_A};
|
|
|
|
/* mag01[x] = x * MATRIX_A for x=0,1 */
|
|
|
|
unsigned long *mt;
|
|
|
|
|
|
|
|
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);
|
|
|
|
mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL];
|
|
|
|
}
|
|
|
|
for (;kk<N-1;kk++) {
|
|
|
|
y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
|
|
|
|
mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
|
2003-04-16 15:40:04 -03:00
|
|
|
}
|
2002-12-29 19:03:38 -04:00
|
|
|
y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
|
|
|
|
mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];
|
|
|
|
|
|
|
|
self->index = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
y = mt[self->index++];
|
|
|
|
y ^= (y >> 11);
|
|
|
|
y ^= (y << 7) & 0x9d2c5680UL;
|
|
|
|
y ^= (y << 15) & 0xefc60000UL;
|
|
|
|
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.
|
|
|
|
* The orginal code credited Isaku Wada for this algorithm, 2002/01/09.
|
|
|
|
*/
|
|
|
|
static PyObject *
|
|
|
|
random_random(RandomObject *self)
|
|
|
|
{
|
|
|
|
unsigned long a=genrand_int32(self)>>5, b=genrand_int32(self)>>6;
|
|
|
|
return PyFloat_FromDouble((a*67108864.0+b)*(1.0/9007199254740992.0));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* initializes mt[N] with a seed */
|
|
|
|
static void
|
|
|
|
init_genrand(RandomObject *self, unsigned long s)
|
|
|
|
{
|
|
|
|
int mti;
|
|
|
|
unsigned long *mt;
|
|
|
|
|
|
|
|
mt = self->state;
|
|
|
|
mt[0]= s & 0xffffffffUL;
|
|
|
|
for (mti=1; mti<N; mti++) {
|
|
|
|
mt[mti] =
|
|
|
|
(1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
|
|
|
|
/* 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 */
|
|
|
|
mt[mti] &= 0xffffffffUL;
|
|
|
|
/* for >32 bit machines */
|
|
|
|
}
|
|
|
|
self->index = mti;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* initialize by an array with array-length */
|
|
|
|
/* init_key is the array for initializing keys */
|
|
|
|
/* key_length is its length */
|
|
|
|
static PyObject *
|
|
|
|
init_by_array(RandomObject *self, unsigned long init_key[], unsigned long key_length)
|
|
|
|
{
|
|
|
|
unsigned int i, j, k; /* was signed in the original code. RDH 12/16/2002 */
|
|
|
|
unsigned long *mt;
|
|
|
|
|
|
|
|
mt = self->state;
|
|
|
|
init_genrand(self, 19650218UL);
|
|
|
|
i=1; j=0;
|
|
|
|
k = (N>key_length ? N : key_length);
|
|
|
|
for (; k; k--) {
|
|
|
|
mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL))
|
|
|
|
+ init_key[j] + j; /* non linear */
|
|
|
|
mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
|
|
|
|
i++; j++;
|
|
|
|
if (i>=N) { mt[0] = mt[N-1]; i=1; }
|
|
|
|
if (j>=key_length) j=0;
|
|
|
|
}
|
|
|
|
for (k=N-1; k; k--) {
|
|
|
|
mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL))
|
|
|
|
- i; /* non linear */
|
|
|
|
mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
|
|
|
|
i++;
|
|
|
|
if (i>=N) { mt[0] = mt[N-1]; i=1; }
|
|
|
|
}
|
|
|
|
|
|
|
|
mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The rest is Python-specific code, neither part of, nor derived from, the
|
|
|
|
* Twister download.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
random_seed(RandomObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
PyObject *result = NULL; /* guilty until proved innocent */
|
|
|
|
PyObject *masklower = NULL;
|
|
|
|
PyObject *thirtytwo = NULL;
|
|
|
|
PyObject *n = NULL;
|
|
|
|
unsigned long *key = NULL;
|
|
|
|
unsigned long keymax; /* # of allocated slots in key */
|
|
|
|
unsigned long keyused; /* # of used slots in key */
|
2003-04-22 21:14:18 -03:00
|
|
|
int err;
|
2002-12-29 19:03:38 -04:00
|
|
|
|
|
|
|
PyObject *arg = NULL;
|
|
|
|
|
|
|
|
if (!PyArg_UnpackTuple(args, "seed", 0, 1, &arg))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (arg == NULL || arg == Py_None) {
|
|
|
|
time_t now;
|
|
|
|
|
|
|
|
time(&now);
|
|
|
|
init_genrand(self, (unsigned long)now);
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
/* If the arg is an int or long, use its absolute value; else use
|
|
|
|
* the absolute value of its hash code.
|
|
|
|
*/
|
2007-08-31 01:32:55 -03:00
|
|
|
if (PyLong_Check(arg))
|
2002-12-29 19:03:38 -04:00
|
|
|
n = PyNumber_Absolute(arg);
|
|
|
|
else {
|
|
|
|
long hash = PyObject_Hash(arg);
|
|
|
|
if (hash == -1)
|
|
|
|
goto Done;
|
|
|
|
n = PyLong_FromUnsignedLong((unsigned long)hash);
|
|
|
|
}
|
|
|
|
if (n == NULL)
|
|
|
|
goto Done;
|
|
|
|
|
|
|
|
/* Now split n into 32-bit chunks, from the right. Each piece is
|
|
|
|
* stored into key, which has a capacity of keymax chunks, of which
|
|
|
|
* keyused are filled. Alas, the repeated shifting makes this a
|
|
|
|
* quadratic-time algorithm; we'd really like to use
|
|
|
|
* _PyLong_AsByteArray here, but then we'd have to break into the
|
|
|
|
* long representation to figure out how big an array was needed
|
|
|
|
* in advance.
|
|
|
|
*/
|
|
|
|
keymax = 8; /* arbitrary; grows later if needed */
|
|
|
|
keyused = 0;
|
|
|
|
key = (unsigned long *)PyMem_Malloc(keymax * sizeof(*key));
|
|
|
|
if (key == NULL)
|
|
|
|
goto Done;
|
|
|
|
|
|
|
|
masklower = PyLong_FromUnsignedLong(0xffffffffU);
|
|
|
|
if (masklower == NULL)
|
|
|
|
goto Done;
|
2007-12-02 10:31:20 -04:00
|
|
|
thirtytwo = PyLong_FromLong(32L);
|
2002-12-29 19:03:38 -04:00
|
|
|
if (thirtytwo == NULL)
|
|
|
|
goto Done;
|
2003-04-22 21:14:18 -03:00
|
|
|
while ((err=PyObject_IsTrue(n))) {
|
2002-12-29 19:03:38 -04:00
|
|
|
PyObject *newn;
|
|
|
|
PyObject *pychunk;
|
|
|
|
unsigned long chunk;
|
|
|
|
|
2003-04-22 21:14:18 -03:00
|
|
|
if (err == -1)
|
|
|
|
goto Done;
|
2002-12-29 19:03:38 -04:00
|
|
|
pychunk = PyNumber_And(n, masklower);
|
|
|
|
if (pychunk == NULL)
|
|
|
|
goto Done;
|
|
|
|
chunk = PyLong_AsUnsignedLong(pychunk);
|
|
|
|
Py_DECREF(pychunk);
|
|
|
|
if (chunk == (unsigned long)-1 && PyErr_Occurred())
|
|
|
|
goto Done;
|
|
|
|
newn = PyNumber_Rshift(n, thirtytwo);
|
|
|
|
if (newn == NULL)
|
|
|
|
goto Done;
|
|
|
|
Py_DECREF(n);
|
|
|
|
n = newn;
|
|
|
|
if (keyused >= keymax) {
|
|
|
|
unsigned long bigger = keymax << 1;
|
|
|
|
if ((bigger >> 1) != keymax) {
|
|
|
|
PyErr_NoMemory();
|
|
|
|
goto Done;
|
|
|
|
}
|
|
|
|
key = (unsigned long *)PyMem_Realloc(key,
|
|
|
|
bigger * sizeof(*key));
|
|
|
|
if (key == NULL)
|
|
|
|
goto Done;
|
|
|
|
keymax = bigger;
|
|
|
|
}
|
|
|
|
assert(keyused < keymax);
|
|
|
|
key[keyused++] = chunk;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (keyused == 0)
|
|
|
|
key[keyused++] = 0UL;
|
|
|
|
result = init_by_array(self, key, keyused);
|
|
|
|
Done:
|
|
|
|
Py_XDECREF(masklower);
|
|
|
|
Py_XDECREF(thirtytwo);
|
|
|
|
Py_XDECREF(n);
|
|
|
|
PyMem_Free(key);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
random_getstate(RandomObject *self)
|
|
|
|
{
|
|
|
|
PyObject *state;
|
|
|
|
PyObject *element;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
state = PyTuple_New(N+1);
|
|
|
|
if (state == NULL)
|
|
|
|
return NULL;
|
|
|
|
for (i=0; i<N ; i++) {
|
2007-12-02 10:31:20 -04:00
|
|
|
element = PyLong_FromLong((long)(self->state[i]));
|
2002-12-29 19:03:38 -04:00
|
|
|
if (element == NULL)
|
|
|
|
goto Fail;
|
|
|
|
PyTuple_SET_ITEM(state, i, element);
|
|
|
|
}
|
2007-12-02 10:31:20 -04:00
|
|
|
element = PyLong_FromLong((long)(self->index));
|
2002-12-29 19:03:38 -04:00
|
|
|
if (element == NULL)
|
|
|
|
goto Fail;
|
|
|
|
PyTuple_SET_ITEM(state, i, element);
|
|
|
|
return state;
|
|
|
|
|
|
|
|
Fail:
|
|
|
|
Py_DECREF(state);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
random_setstate(RandomObject *self, PyObject *state)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
long element;
|
|
|
|
|
|
|
|
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++) {
|
2007-12-02 10:31:20 -04:00
|
|
|
element = PyLong_AsLong(PyTuple_GET_ITEM(state, i));
|
2002-12-29 19:03:38 -04:00
|
|
|
if (element == -1 && PyErr_Occurred())
|
|
|
|
return NULL;
|
|
|
|
self->state[i] = (unsigned long)element;
|
|
|
|
}
|
|
|
|
|
2007-12-02 10:31:20 -04:00
|
|
|
element = PyLong_AsLong(PyTuple_GET_ITEM(state, i));
|
2002-12-29 19:03:38 -04:00
|
|
|
if (element == -1 && PyErr_Occurred())
|
|
|
|
return NULL;
|
|
|
|
self->index = (int)element;
|
|
|
|
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Jumpahead should be a fast way advance the generator n-steps ahead, but
|
|
|
|
lacking a formula for that, the next best is to use n and the existing
|
|
|
|
state to create a new state far away from the original.
|
|
|
|
|
|
|
|
The generator uses constant spaced additive feedback, so shuffling the
|
|
|
|
state elements ought to produce a state which would not be encountered
|
|
|
|
(in the near term) by calls to random(). Shuffling is normally
|
|
|
|
implemented by swapping the ith element with another element ranging
|
|
|
|
from 0 to i inclusive. That allows the element to have the possibility
|
|
|
|
of not being moved. Since the goal is to produce a new, different
|
|
|
|
state, the swap element is ranged from 0 to i-1 inclusive. This assures
|
|
|
|
that each element gets moved at least once.
|
|
|
|
|
|
|
|
To make sure that consecutive calls to jumpahead(n) produce different
|
|
|
|
states (even in the rare case of involutory shuffles), i+1 is added to
|
|
|
|
each element at position i. Successive calls are then guaranteed to
|
|
|
|
have changing (growing) values as well as shuffled positions.
|
|
|
|
|
|
|
|
Finally, the self->index value is set to N so that the generator itself
|
|
|
|
kicks in on the next call to random(). This assures that all results
|
|
|
|
have been through the generator and do not just reflect alterations to
|
|
|
|
the underlying state.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
random_jumpahead(RandomObject *self, PyObject *n)
|
|
|
|
{
|
|
|
|
long i, j;
|
|
|
|
PyObject *iobj;
|
|
|
|
PyObject *remobj;
|
|
|
|
unsigned long *mt, tmp;
|
|
|
|
|
2007-08-31 01:32:55 -03:00
|
|
|
if (!PyLong_Check(n)) {
|
2002-12-29 19:03:38 -04:00
|
|
|
PyErr_Format(PyExc_TypeError, "jumpahead requires an "
|
|
|
|
"integer, not '%s'",
|
Merged revisions 56467-56482 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/p3yk
................
r56477 | martin.v.loewis | 2007-07-21 09:04:38 +0200 (Sa, 21 Jul 2007) | 11 lines
Merged revisions 56466-56476 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r56476 | martin.v.loewis | 2007-07-21 08:55:02 +0200 (Sa, 21 Jul 2007) | 4 lines
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
backwards compatibility. Add Py_Refcnt, Py_Type, Py_Size, and
PyVarObject_HEAD_INIT.
........
................
r56478 | martin.v.loewis | 2007-07-21 09:47:23 +0200 (Sa, 21 Jul 2007) | 2 lines
PEP 3123: Use proper C inheritance for PyObject.
................
r56479 | martin.v.loewis | 2007-07-21 10:06:55 +0200 (Sa, 21 Jul 2007) | 3 lines
Add longintrepr.h to Python.h, so that the compiler can
see that PyFalse is really some kind of PyObject*.
................
r56480 | martin.v.loewis | 2007-07-21 10:47:18 +0200 (Sa, 21 Jul 2007) | 2 lines
Qualify SHIFT, MASK, BASE.
................
r56482 | martin.v.loewis | 2007-07-21 19:10:57 +0200 (Sa, 21 Jul 2007) | 2 lines
Correctly refer to _ob_next.
................
2007-07-21 14:22:18 -03:00
|
|
|
Py_Type(n)->tp_name);
|
2002-12-29 19:03:38 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
mt = self->state;
|
|
|
|
for (i = N-1; i > 1; i--) {
|
2007-12-02 10:31:20 -04:00
|
|
|
iobj = PyLong_FromLong(i);
|
2002-12-29 19:03:38 -04:00
|
|
|
if (iobj == NULL)
|
|
|
|
return NULL;
|
|
|
|
remobj = PyNumber_Remainder(n, iobj);
|
|
|
|
Py_DECREF(iobj);
|
|
|
|
if (remobj == NULL)
|
|
|
|
return NULL;
|
2007-12-02 10:31:20 -04:00
|
|
|
j = PyLong_AsLong(remobj);
|
2002-12-29 19:03:38 -04:00
|
|
|
Py_DECREF(remobj);
|
|
|
|
if (j == -1L && PyErr_Occurred())
|
|
|
|
return NULL;
|
|
|
|
tmp = mt[i];
|
|
|
|
mt[i] = mt[j];
|
|
|
|
mt[j] = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < N; i++)
|
|
|
|
mt[i] += i+1;
|
|
|
|
|
|
|
|
self->index = N;
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
|
2003-10-05 06:09:15 -03:00
|
|
|
static PyObject *
|
|
|
|
random_getrandbits(RandomObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
int k, i, bytes;
|
|
|
|
unsigned long r;
|
|
|
|
unsigned char *bytearray;
|
|
|
|
PyObject *result;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "i:getrandbits", &k))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (k <= 0) {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"number of bits must be greater than zero");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes = ((k - 1) / 32 + 1) * 4;
|
|
|
|
bytearray = (unsigned char *)PyMem_Malloc(bytes);
|
|
|
|
if (bytearray == NULL) {
|
|
|
|
PyErr_NoMemory();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fill-out whole words, byte-by-byte to avoid endianness issues */
|
|
|
|
for (i=0 ; i<bytes ; i+=4, k-=32) {
|
|
|
|
r = genrand_int32(self);
|
|
|
|
if (k < 32)
|
|
|
|
r >>= (32 - k);
|
|
|
|
bytearray[i+0] = (unsigned char)r;
|
|
|
|
bytearray[i+1] = (unsigned char)(r >> 8);
|
|
|
|
bytearray[i+2] = (unsigned char)(r >> 16);
|
|
|
|
bytearray[i+3] = (unsigned char)(r >> 24);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* little endian order to match bytearray assignment order */
|
|
|
|
result = _PyLong_FromByteArray(bytearray, bytes, 1, 0);
|
|
|
|
PyMem_Free(bytearray);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2002-12-29 19:03:38 -04:00
|
|
|
static PyObject *
|
|
|
|
random_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
|
|
|
{
|
|
|
|
RandomObject *self;
|
|
|
|
PyObject *tmp;
|
|
|
|
|
Merged revisions 53451-53537 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r53454 | brett.cannon | 2007-01-15 20:12:08 +0100 (Mon, 15 Jan 2007) | 3 lines
Add a note for strptime that just because strftime supports some extra
directive that is not documented that strptime will as well.
........
r53458 | vinay.sajip | 2007-01-16 10:50:07 +0100 (Tue, 16 Jan 2007) | 1 line
Updated rotating file handlers to use _open().
........
r53459 | marc-andre.lemburg | 2007-01-16 14:03:06 +0100 (Tue, 16 Jan 2007) | 2 lines
Add news items for the recent pybench and platform changes.
........
r53460 | sjoerd.mullender | 2007-01-16 17:42:38 +0100 (Tue, 16 Jan 2007) | 4 lines
Fixed ntpath.expandvars to not replace references to non-existing
variables with nothing. Also added tests.
This fixes bug #494589.
........
r53464 | neal.norwitz | 2007-01-17 07:23:51 +0100 (Wed, 17 Jan 2007) | 1 line
Give Calvin Spealman access for python-dev summaries.
........
r53465 | neal.norwitz | 2007-01-17 09:37:26 +0100 (Wed, 17 Jan 2007) | 1 line
Remove Calvin since he only has access to the website currently.
........
r53466 | thomas.heller | 2007-01-17 10:40:34 +0100 (Wed, 17 Jan 2007) | 2 lines
Replace C++ comments with C comments.
........
r53472 | andrew.kuchling | 2007-01-17 20:55:06 +0100 (Wed, 17 Jan 2007) | 1 line
[Part of bug #1599254] Add suggestion to Mailbox docs to use Maildir, and warn user to lock/unlock mailboxes when modifying them
........
r53475 | georg.brandl | 2007-01-17 22:09:04 +0100 (Wed, 17 Jan 2007) | 2 lines
Bug #1637967: missing //= operator in list.
........
r53477 | georg.brandl | 2007-01-17 22:19:58 +0100 (Wed, 17 Jan 2007) | 2 lines
Bug #1629125: fix wrong data type (int -> Py_ssize_t) in PyDict_Next docs.
........
r53481 | neal.norwitz | 2007-01-18 06:40:58 +0100 (Thu, 18 Jan 2007) | 1 line
Try reverting part of r53145 that seems to cause the Windows buildbots to fail in test_uu.UUFileTest.test_encode
........
r53482 | fred.drake | 2007-01-18 06:42:30 +0100 (Thu, 18 Jan 2007) | 1 line
add missing version entry
........
r53483 | neal.norwitz | 2007-01-18 07:20:55 +0100 (Thu, 18 Jan 2007) | 7 lines
This test doesn't pass on Windows. The cause seems to be that chmod
doesn't support the same funcationality as on Unix. I'm not sure if
this fix is the best (or if it will even work)--it's a test to see
if the buildbots start passing again.
It might be better to not even run this test if it's windows (or non-posix).
........
r53488 | neal.norwitz | 2007-01-19 06:53:33 +0100 (Fri, 19 Jan 2007) | 1 line
SF #1635217, Fix unbalanced paren
........
r53489 | martin.v.loewis | 2007-01-19 07:42:22 +0100 (Fri, 19 Jan 2007) | 3 lines
Prefix AST symbols with _Py_. Fixes #1637022.
Will backport.
........
r53497 | martin.v.loewis | 2007-01-19 19:01:38 +0100 (Fri, 19 Jan 2007) | 2 lines
Add UUIDs for 2.5.1 and 2.5.2
........
r53499 | raymond.hettinger | 2007-01-19 19:07:18 +0100 (Fri, 19 Jan 2007) | 1 line
SF# 1635892: Fix docs for betavariate's input parameters .
........
r53503 | martin.v.loewis | 2007-01-20 15:05:39 +0100 (Sat, 20 Jan 2007) | 2 lines
Merge 53501 and 53502 from 25 branch:
Add /GS- for AMD64 and Itanium builds where missing.
........
r53504 | walter.doerwald | 2007-01-20 18:28:31 +0100 (Sat, 20 Jan 2007) | 2 lines
Port test_resource.py to unittest.
........
r53505 | walter.doerwald | 2007-01-20 19:19:33 +0100 (Sat, 20 Jan 2007) | 2 lines
Add argument tests an calls of resource.getrusage().
........
r53506 | walter.doerwald | 2007-01-20 20:03:17 +0100 (Sat, 20 Jan 2007) | 2 lines
resource.RUSAGE_BOTH might not exist.
........
r53507 | walter.doerwald | 2007-01-21 00:07:28 +0100 (Sun, 21 Jan 2007) | 2 lines
Port test_new.py to unittest.
........
r53508 | martin.v.loewis | 2007-01-21 10:33:07 +0100 (Sun, 21 Jan 2007) | 2 lines
Patch #1610575: Add support for _Bool to struct.
........
r53509 | georg.brandl | 2007-01-21 11:28:43 +0100 (Sun, 21 Jan 2007) | 3 lines
Bug #1486663: don't reject keyword arguments for subclasses of builtin
types.
........
r53511 | georg.brandl | 2007-01-21 11:35:10 +0100 (Sun, 21 Jan 2007) | 2 lines
Patch #1627441: close sockets properly in urllib2.
........
r53517 | georg.brandl | 2007-01-22 20:40:21 +0100 (Mon, 22 Jan 2007) | 3 lines
Use new email module names (#1637162, #1637159, #1637157).
........
r53518 | andrew.kuchling | 2007-01-22 21:26:40 +0100 (Mon, 22 Jan 2007) | 1 line
Improve pattern used for mbox 'From' lines; add a simple test
........
r53519 | andrew.kuchling | 2007-01-22 21:27:50 +0100 (Mon, 22 Jan 2007) | 1 line
Make comment match the code
........
r53522 | georg.brandl | 2007-01-22 22:10:33 +0100 (Mon, 22 Jan 2007) | 2 lines
Bug #1249573: fix rfc822.parsedate not accepting a certain date format
........
r53524 | georg.brandl | 2007-01-22 22:23:41 +0100 (Mon, 22 Jan 2007) | 2 lines
Bug #1627316: handle error in condition/ignore pdb commands more gracefully.
........
r53526 | lars.gustaebel | 2007-01-23 12:17:33 +0100 (Tue, 23 Jan 2007) | 4 lines
Patch #1507247: tarfile.py: use current umask for intermediate
directories.
........
r53527 | thomas.wouters | 2007-01-23 14:42:00 +0100 (Tue, 23 Jan 2007) | 13 lines
SF patch #1630975: Fix crash when replacing sys.stdout in sitecustomize
When running the interpreter in an environment that would cause it to set
stdout/stderr/stdin's encoding, having a sitecustomize that would replace
them with something other than PyFile objects would crash the interpreter.
Fix it by simply ignoring the encoding-setting for non-files.
This could do with a test, but I can think of no maintainable and portable
way to test this bug, short of adding a sitecustomize.py to the buildsystem
and have it always run with it (hmmm....)
........
r53528 | thomas.wouters | 2007-01-23 14:50:49 +0100 (Tue, 23 Jan 2007) | 4 lines
Add news entry about last checkin (oops.)
........
r53531 | martin.v.loewis | 2007-01-23 22:11:47 +0100 (Tue, 23 Jan 2007) | 4 lines
Make PyTraceBack_Here use the current thread, not the
frame's thread state. Fixes #1579370.
Will backport.
........
r53535 | brett.cannon | 2007-01-24 00:21:22 +0100 (Wed, 24 Jan 2007) | 5 lines
Fix crasher for when an object's __del__ creates a new weakref to itself.
Patch only fixes new-style classes; classic classes still buggy.
Closes bug #1377858. Already backported.
........
r53536 | walter.doerwald | 2007-01-24 01:42:19 +0100 (Wed, 24 Jan 2007) | 2 lines
Port test_popen.py to unittest.
........
2007-02-01 14:02:27 -04:00
|
|
|
if (type == &Random_Type && !_PyArg_NoKeywords("Random()", kwds))
|
2005-08-26 03:42:30 -03:00
|
|
|
return NULL;
|
|
|
|
|
2002-12-29 19:03:38 -04:00
|
|
|
self = (RandomObject *)type->tp_alloc(type, 0);
|
|
|
|
if (self == NULL)
|
|
|
|
return NULL;
|
|
|
|
tmp = random_seed(self, args);
|
|
|
|
if (tmp == NULL) {
|
|
|
|
Py_DECREF(self);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_DECREF(tmp);
|
|
|
|
return (PyObject *)self;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyMethodDef random_methods[] = {
|
|
|
|
{"random", (PyCFunction)random_random, METH_NOARGS,
|
|
|
|
PyDoc_STR("random() -> x in the interval [0, 1).")},
|
|
|
|
{"seed", (PyCFunction)random_seed, METH_VARARGS,
|
|
|
|
PyDoc_STR("seed([n]) -> None. Defaults to current time.")},
|
|
|
|
{"getstate", (PyCFunction)random_getstate, METH_NOARGS,
|
|
|
|
PyDoc_STR("getstate() -> tuple containing the current state.")},
|
|
|
|
{"setstate", (PyCFunction)random_setstate, METH_O,
|
|
|
|
PyDoc_STR("setstate(state) -> None. Restores generator state.")},
|
|
|
|
{"jumpahead", (PyCFunction)random_jumpahead, METH_O,
|
|
|
|
PyDoc_STR("jumpahead(int) -> None. Create new state from "
|
|
|
|
"existing state and integer.")},
|
2003-10-05 06:09:15 -03:00
|
|
|
{"getrandbits", (PyCFunction)random_getrandbits, METH_VARARGS,
|
|
|
|
PyDoc_STR("getrandbits(k) -> x. Generates a long int with "
|
|
|
|
"k random bits.")},
|
2002-12-29 19:03:38 -04:00
|
|
|
{NULL, NULL} /* sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
PyDoc_STRVAR(random_doc,
|
|
|
|
"Random() -> create a random number generator with its own internal state.");
|
|
|
|
|
|
|
|
static PyTypeObject Random_Type = {
|
Merged revisions 56467-56482 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/p3yk
................
r56477 | martin.v.loewis | 2007-07-21 09:04:38 +0200 (Sa, 21 Jul 2007) | 11 lines
Merged revisions 56466-56476 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r56476 | martin.v.loewis | 2007-07-21 08:55:02 +0200 (Sa, 21 Jul 2007) | 4 lines
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
backwards compatibility. Add Py_Refcnt, Py_Type, Py_Size, and
PyVarObject_HEAD_INIT.
........
................
r56478 | martin.v.loewis | 2007-07-21 09:47:23 +0200 (Sa, 21 Jul 2007) | 2 lines
PEP 3123: Use proper C inheritance for PyObject.
................
r56479 | martin.v.loewis | 2007-07-21 10:06:55 +0200 (Sa, 21 Jul 2007) | 3 lines
Add longintrepr.h to Python.h, so that the compiler can
see that PyFalse is really some kind of PyObject*.
................
r56480 | martin.v.loewis | 2007-07-21 10:47:18 +0200 (Sa, 21 Jul 2007) | 2 lines
Qualify SHIFT, MASK, BASE.
................
r56482 | martin.v.loewis | 2007-07-21 19:10:57 +0200 (Sa, 21 Jul 2007) | 2 lines
Correctly refer to _ob_next.
................
2007-07-21 14:22:18 -03:00
|
|
|
PyVarObject_HEAD_INIT(NULL, 0)
|
2002-12-29 19:03:38 -04:00
|
|
|
"_random.Random", /*tp_name*/
|
|
|
|
sizeof(RandomObject), /*tp_basicsize*/
|
|
|
|
0, /*tp_itemsize*/
|
|
|
|
/* methods */
|
|
|
|
0, /*tp_dealloc*/
|
|
|
|
0, /*tp_print*/
|
|
|
|
0, /*tp_getattr*/
|
|
|
|
0, /*tp_setattr*/
|
|
|
|
0, /*tp_compare*/
|
|
|
|
0, /*tp_repr*/
|
|
|
|
0, /*tp_as_number*/
|
|
|
|
0, /*tp_as_sequence*/
|
|
|
|
0, /*tp_as_mapping*/
|
|
|
|
0, /*tp_hash*/
|
|
|
|
0, /*tp_call*/
|
|
|
|
0, /*tp_str*/
|
2003-01-06 08:41:26 -04:00
|
|
|
PyObject_GenericGetAttr, /*tp_getattro*/
|
2002-12-29 19:03:38 -04:00
|
|
|
0, /*tp_setattro*/
|
|
|
|
0, /*tp_as_buffer*/
|
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
|
|
|
|
random_doc, /*tp_doc*/
|
|
|
|
0, /*tp_traverse*/
|
|
|
|
0, /*tp_clear*/
|
|
|
|
0, /*tp_richcompare*/
|
|
|
|
0, /*tp_weaklistoffset*/
|
|
|
|
0, /*tp_iter*/
|
|
|
|
0, /*tp_iternext*/
|
|
|
|
random_methods, /*tp_methods*/
|
|
|
|
0, /*tp_members*/
|
|
|
|
0, /*tp_getset*/
|
|
|
|
0, /*tp_base*/
|
|
|
|
0, /*tp_dict*/
|
|
|
|
0, /*tp_descr_get*/
|
|
|
|
0, /*tp_descr_set*/
|
|
|
|
0, /*tp_dictoffset*/
|
|
|
|
0, /*tp_init*/
|
2003-05-23 00:55:42 -03:00
|
|
|
0, /*tp_alloc*/
|
2002-12-29 19:03:38 -04:00
|
|
|
random_new, /*tp_new*/
|
2007-08-19 19:48:23 -03:00
|
|
|
PyObject_Free, /*tp_free*/
|
2002-12-29 19:03:38 -04:00
|
|
|
0, /*tp_is_gc*/
|
|
|
|
};
|
|
|
|
|
|
|
|
PyDoc_STRVAR(module_doc,
|
|
|
|
"Module implements the Mersenne Twister random number generator.");
|
|
|
|
|
|
|
|
PyMODINIT_FUNC
|
|
|
|
init_random(void)
|
|
|
|
{
|
|
|
|
PyObject *m;
|
|
|
|
|
|
|
|
if (PyType_Ready(&Random_Type) < 0)
|
|
|
|
return;
|
|
|
|
m = Py_InitModule3("_random", NULL, module_doc);
|
2006-01-19 02:09:39 -04:00
|
|
|
if (m == NULL)
|
|
|
|
return;
|
2002-12-29 19:03:38 -04:00
|
|
|
Py_INCREF(&Random_Type);
|
|
|
|
PyModule_AddObject(m, "Random", (PyObject *)&Random_Type);
|
|
|
|
}
|