]> git.proxmox.com Git - mirror_edk2.git/blobdiff - AppPkg/Applications/Python/Python-2.7.10/Modules/_randommodule.c
AppPkg/Applications/Python/Python-2.7.10: Initial Checkin part 2/5.
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.10 / Modules / _randommodule.c
diff --git a/AppPkg/Applications/Python/Python-2.7.10/Modules/_randommodule.c b/AppPkg/Applications/Python/Python-2.7.10/Modules/_randommodule.c
new file mode 100644 (file)
index 0000000..1033965
--- /dev/null
@@ -0,0 +1,595 @@
+/* Random objects */\r
+\r
+/* ------------------------------------------------------------------\r
+   The code in this module was based on a download from:\r
+      http://www.math.keio.ac.jp/~matumoto/MT2002/emt19937ar.html\r
+\r
+   It was modified in 2002 by Raymond Hettinger as follows:\r
+\r
+    * the principal computational lines untouched.\r
+\r
+    * renamed genrand_res53() to random_random() and wrapped\r
+      in python calling/return code.\r
+\r
+    * genrand_int32() and the helper functions, init_genrand()\r
+      and init_by_array(), were declared static, wrapped in\r
+      Python calling/return code.  also, their global data\r
+      references were replaced with structure references.\r
+\r
+    * unused functions from the original were deleted.\r
+      new, original C python code was added to implement the\r
+      Random() interface.\r
+\r
+   The following are the verbatim comments from the original code:\r
+\r
+   A C-program for MT19937, with initialization improved 2002/1/26.\r
+   Coded by Takuji Nishimura and Makoto Matsumoto.\r
+\r
+   Before using, initialize the state by using init_genrand(seed)\r
+   or init_by_array(init_key, key_length).\r
+\r
+   Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,\r
+   All rights reserved.\r
+\r
+   Redistribution and use in source and binary forms, with or without\r
+   modification, are permitted provided that the following conditions\r
+   are met:\r
+\r
+     1. Redistributions of source code must retain the above copyright\r
+    notice, this list of conditions and the following disclaimer.\r
+\r
+     2. Redistributions in binary form must reproduce the above copyright\r
+    notice, this list of conditions and the following disclaimer in the\r
+    documentation and/or other materials provided with the distribution.\r
+\r
+     3. The names of its contributors may not be used to endorse or promote\r
+    products derived from this software without specific prior written\r
+    permission.\r
+\r
+   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\r
+   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\r
+   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\r
+   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR\r
+   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,\r
+   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,\r
+   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\r
+   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\r
+   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
+   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
+   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+\r
+\r
+   Any feedback is very welcome.\r
+   http://www.math.keio.ac.jp/matumoto/emt.html\r
+   email: matumoto@math.keio.ac.jp\r
+*/\r
+\r
+/* ---------------------------------------------------------------*/\r
+\r
+#include "Python.h"\r
+#include <time.h>               /* for seeding to current time */\r
+\r
+/* Period parameters -- These are all magic.  Don't change. */\r
+#define N 624\r
+#define M 397\r
+#define MATRIX_A 0x9908b0dfUL   /* constant vector a */\r
+#define UPPER_MASK 0x80000000UL /* most significant w-r bits */\r
+#define LOWER_MASK 0x7fffffffUL /* least significant r bits */\r
+\r
+typedef struct {\r
+    PyObject_HEAD\r
+    unsigned long state[N];\r
+    int index;\r
+} RandomObject;\r
+\r
+static PyTypeObject Random_Type;\r
+\r
+#define RandomObject_Check(v)      (Py_TYPE(v) == &Random_Type)\r
+\r
+\r
+/* Random methods */\r
+\r
+\r
+/* generates a random number on [0,0xffffffff]-interval */\r
+static unsigned long\r
+genrand_int32(RandomObject *self)\r
+{\r
+    unsigned long y;\r
+    static unsigned long mag01[2]={0x0UL, MATRIX_A};\r
+    /* mag01[x] = x * MATRIX_A  for x=0,1 */\r
+    unsigned long *mt;\r
+\r
+    mt = self->state;\r
+    if (self->index >= N) { /* generate N words at one time */\r
+        int kk;\r
+\r
+        for (kk=0;kk<N-M;kk++) {\r
+            y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);\r
+            mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL];\r
+        }\r
+        for (;kk<N-1;kk++) {\r
+            y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);\r
+            mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL];\r
+        }\r
+        y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);\r
+        mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];\r
+\r
+        self->index = 0;\r
+    }\r
+\r
+    y = mt[self->index++];\r
+    y ^= (y >> 11);\r
+    y ^= (y << 7) & 0x9d2c5680UL;\r
+    y ^= (y << 15) & 0xefc60000UL;\r
+    y ^= (y >> 18);\r
+    return y;\r
+}\r
+\r
+/* random_random is the function named genrand_res53 in the original code;\r
+ * generates a random number on [0,1) with 53-bit resolution; note that\r
+ * 9007199254740992 == 2**53; I assume they're spelling "/2**53" as\r
+ * multiply-by-reciprocal in the (likely vain) hope that the compiler will\r
+ * optimize the division away at compile-time.  67108864 is 2**26.  In\r
+ * effect, a contains 27 random bits shifted left 26, and b fills in the\r
+ * lower 26 bits of the 53-bit numerator.\r
+ * The orginal code credited Isaku Wada for this algorithm, 2002/01/09.\r
+ */\r
+static PyObject *\r
+random_random(RandomObject *self)\r
+{\r
+    unsigned long a=genrand_int32(self)>>5, b=genrand_int32(self)>>6;\r
+    return PyFloat_FromDouble((a*67108864.0+b)*(1.0/9007199254740992.0));\r
+}\r
+\r
+/* initializes mt[N] with a seed */\r
+static void\r
+init_genrand(RandomObject *self, unsigned long s)\r
+{\r
+    int mti;\r
+    unsigned long *mt;\r
+\r
+    mt = self->state;\r
+    mt[0]= s & 0xffffffffUL;\r
+    for (mti=1; mti<N; mti++) {\r
+        mt[mti] =\r
+        (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);\r
+        /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */\r
+        /* In the previous versions, MSBs of the seed affect   */\r
+        /* only MSBs of the array mt[].                                */\r
+        /* 2002/01/09 modified by Makoto Matsumoto                     */\r
+        mt[mti] &= 0xffffffffUL;\r
+        /* for >32 bit machines */\r
+    }\r
+    self->index = mti;\r
+    return;\r
+}\r
+\r
+/* initialize by an array with array-length */\r
+/* init_key is the array for initializing keys */\r
+/* key_length is its length */\r
+static PyObject *\r
+init_by_array(RandomObject *self, unsigned long init_key[], unsigned long key_length)\r
+{\r
+    unsigned int i, j, k;       /* was signed in the original code. RDH 12/16/2002 */\r
+    unsigned long *mt;\r
+\r
+    mt = self->state;\r
+    init_genrand(self, 19650218UL);\r
+    i=1; j=0;\r
+    k = (N>key_length ? N : key_length);\r
+    for (; k; k--) {\r
+        mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL))\r
+                 + init_key[j] + j; /* non linear */\r
+        mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */\r
+        i++; j++;\r
+        if (i>=N) { mt[0] = mt[N-1]; i=1; }\r
+        if (j>=key_length) j=0;\r
+    }\r
+    for (k=N-1; k; k--) {\r
+        mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL))\r
+                 - i; /* non linear */\r
+        mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */\r
+        i++;\r
+        if (i>=N) { mt[0] = mt[N-1]; i=1; }\r
+    }\r
+\r
+    mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */\r
+    Py_INCREF(Py_None);\r
+    return Py_None;\r
+}\r
+\r
+/*\r
+ * The rest is Python-specific code, neither part of, nor derived from, the\r
+ * Twister download.\r
+ */\r
+\r
+static PyObject *\r
+random_seed(RandomObject *self, PyObject *args)\r
+{\r
+    PyObject *result = NULL;            /* guilty until proved innocent */\r
+    PyObject *masklower = NULL;\r
+    PyObject *thirtytwo = NULL;\r
+    PyObject *n = NULL;\r
+    unsigned long *key = NULL;\r
+    unsigned long keymax;               /* # of allocated slots in key */\r
+    unsigned long keyused;              /* # of used slots in key */\r
+    int err;\r
+\r
+    PyObject *arg = NULL;\r
+\r
+    if (!PyArg_UnpackTuple(args, "seed", 0, 1, &arg))\r
+        return NULL;\r
+\r
+    if (arg == NULL || arg == Py_None) {\r
+        time_t now;\r
+\r
+        time(&now);\r
+        init_genrand(self, (unsigned long)now);\r
+        Py_INCREF(Py_None);\r
+        return Py_None;\r
+    }\r
+    /* If the arg is an int or long, use its absolute value; else use\r
+     * the absolute value of its hash code.\r
+     */\r
+    if (PyInt_Check(arg) || PyLong_Check(arg))\r
+        n = PyNumber_Absolute(arg);\r
+    else {\r
+        long hash = PyObject_Hash(arg);\r
+        if (hash == -1)\r
+            goto Done;\r
+        n = PyLong_FromUnsignedLong((unsigned long)hash);\r
+    }\r
+    if (n == NULL)\r
+        goto Done;\r
+\r
+    /* Now split n into 32-bit chunks, from the right.  Each piece is\r
+     * stored into key, which has a capacity of keymax chunks, of which\r
+     * keyused are filled.  Alas, the repeated shifting makes this a\r
+     * quadratic-time algorithm; we'd really like to use\r
+     * _PyLong_AsByteArray here, but then we'd have to break into the\r
+     * long representation to figure out how big an array was needed\r
+     * in advance.\r
+     */\r
+    keymax = 8;         /* arbitrary; grows later if needed */\r
+    keyused = 0;\r
+    key = (unsigned long *)PyMem_Malloc(keymax * sizeof(*key));\r
+    if (key == NULL)\r
+        goto Done;\r
+\r
+    masklower = PyLong_FromUnsignedLong(0xffffffffU);\r
+    if (masklower == NULL)\r
+        goto Done;\r
+    thirtytwo = PyInt_FromLong(32L);\r
+    if (thirtytwo == NULL)\r
+        goto Done;\r
+    while ((err=PyObject_IsTrue(n))) {\r
+        PyObject *newn;\r
+        PyObject *pychunk;\r
+        unsigned long chunk;\r
+\r
+        if (err == -1)\r
+            goto Done;\r
+        pychunk = PyNumber_And(n, masklower);\r
+        if (pychunk == NULL)\r
+            goto Done;\r
+        chunk = PyLong_AsUnsignedLong(pychunk);\r
+        Py_DECREF(pychunk);\r
+        if (chunk == (unsigned long)-1 && PyErr_Occurred())\r
+            goto Done;\r
+        newn = PyNumber_Rshift(n, thirtytwo);\r
+        if (newn == NULL)\r
+            goto Done;\r
+        Py_DECREF(n);\r
+        n = newn;\r
+        if (keyused >= keymax) {\r
+            unsigned long bigger = keymax << 1;\r
+            if ((bigger >> 1) != keymax) {\r
+                PyErr_NoMemory();\r
+                goto Done;\r
+            }\r
+            key = (unsigned long *)PyMem_Realloc(key,\r
+                                    bigger * sizeof(*key));\r
+            if (key == NULL)\r
+                goto Done;\r
+            keymax = bigger;\r
+        }\r
+        assert(keyused < keymax);\r
+        key[keyused++] = chunk;\r
+    }\r
+\r
+    if (keyused == 0)\r
+        key[keyused++] = 0UL;\r
+    result = init_by_array(self, key, keyused);\r
+Done:\r
+    Py_XDECREF(masklower);\r
+    Py_XDECREF(thirtytwo);\r
+    Py_XDECREF(n);\r
+    PyMem_Free(key);\r
+    return result;\r
+}\r
+\r
+static PyObject *\r
+random_getstate(RandomObject *self)\r
+{\r
+    PyObject *state;\r
+    PyObject *element;\r
+    int i;\r
+\r
+    state = PyTuple_New(N+1);\r
+    if (state == NULL)\r
+        return NULL;\r
+    for (i=0; i<N ; i++) {\r
+        element = PyLong_FromUnsignedLong(self->state[i]);\r
+        if (element == NULL)\r
+            goto Fail;\r
+        PyTuple_SET_ITEM(state, i, element);\r
+    }\r
+    element = PyLong_FromLong((long)(self->index));\r
+    if (element == NULL)\r
+        goto Fail;\r
+    PyTuple_SET_ITEM(state, i, element);\r
+    return state;\r
+\r
+Fail:\r
+    Py_DECREF(state);\r
+    return NULL;\r
+}\r
+\r
+static PyObject *\r
+random_setstate(RandomObject *self, PyObject *state)\r
+{\r
+    int i;\r
+    unsigned long element;\r
+    long index;\r
+\r
+    if (!PyTuple_Check(state)) {\r
+        PyErr_SetString(PyExc_TypeError,\r
+            "state vector must be a tuple");\r
+        return NULL;\r
+    }\r
+    if (PyTuple_Size(state) != N+1) {\r
+        PyErr_SetString(PyExc_ValueError,\r
+            "state vector is the wrong size");\r
+        return NULL;\r
+    }\r
+\r
+    for (i=0; i<N ; i++) {\r
+        element = PyLong_AsUnsignedLong(PyTuple_GET_ITEM(state, i));\r
+        if (element == (unsigned long)-1 && PyErr_Occurred())\r
+            return NULL;\r
+        self->state[i] = element & 0xffffffffUL; /* Make sure we get sane state */\r
+    }\r
+\r
+    index = PyLong_AsLong(PyTuple_GET_ITEM(state, i));\r
+    if (index == -1 && PyErr_Occurred())\r
+        return NULL;\r
+    self->index = (int)index;\r
+\r
+    Py_INCREF(Py_None);\r
+    return Py_None;\r
+}\r
+\r
+/*\r
+Jumpahead should be a fast way advance the generator n-steps ahead, but\r
+lacking a formula for that, the next best is to use n and the existing\r
+state to create a new state far away from the original.\r
+\r
+The generator uses constant spaced additive feedback, so shuffling the\r
+state elements ought to produce a state which would not be encountered\r
+(in the near term) by calls to random().  Shuffling is normally\r
+implemented by swapping the ith element with another element ranging\r
+from 0 to i inclusive.  That allows the element to have the possibility\r
+of not being moved.  Since the goal is to produce a new, different\r
+state, the swap element is ranged from 0 to i-1 inclusive.  This assures\r
+that each element gets moved at least once.\r
+\r
+To make sure that consecutive calls to jumpahead(n) produce different\r
+states (even in the rare case of involutory shuffles), i+1 is added to\r
+each element at position i.  Successive calls are then guaranteed to\r
+have changing (growing) values as well as shuffled positions.\r
+\r
+Finally, the self->index value is set to N so that the generator itself\r
+kicks in on the next call to random().  This assures that all results\r
+have been through the generator and do not just reflect alterations to\r
+the underlying state.\r
+*/\r
+\r
+static PyObject *\r
+random_jumpahead(RandomObject *self, PyObject *n)\r
+{\r
+    long i, j;\r
+    PyObject *iobj;\r
+    PyObject *remobj;\r
+    unsigned long *mt, tmp, nonzero;\r
+\r
+    if (!PyInt_Check(n) && !PyLong_Check(n)) {\r
+        PyErr_Format(PyExc_TypeError, "jumpahead requires an "\r
+                     "integer, not '%s'",\r
+                     Py_TYPE(n)->tp_name);\r
+        return NULL;\r
+    }\r
+\r
+    mt = self->state;\r
+    for (i = N-1; i > 1; i--) {\r
+        iobj = PyInt_FromLong(i);\r
+        if (iobj == NULL)\r
+            return NULL;\r
+        remobj = PyNumber_Remainder(n, iobj);\r
+        Py_DECREF(iobj);\r
+        if (remobj == NULL)\r
+            return NULL;\r
+        j = PyInt_AsLong(remobj);\r
+        Py_DECREF(remobj);\r
+        if (j == -1L && PyErr_Occurred())\r
+            return NULL;\r
+        tmp = mt[i];\r
+        mt[i] = mt[j];\r
+        mt[j] = tmp;\r
+    }\r
+\r
+    nonzero = 0;\r
+    for (i = 1; i < N; i++) {\r
+        mt[i] += i+1;\r
+        mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */\r
+        nonzero |= mt[i];\r
+    }\r
+\r
+    /* Ensure the state is nonzero: in the unlikely event that mt[1] through\r
+       mt[N-1] are all zero, set the MSB of mt[0] (see issue #14591). In the\r
+       normal case, we fall back to the pre-issue 14591 behaviour for mt[0]. */\r
+    if (nonzero) {\r
+        mt[0] += 1;\r
+        mt[0] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */\r
+    }\r
+    else {\r
+        mt[0] = 0x80000000UL;\r
+    }\r
+\r
+    self->index = N;\r
+    Py_INCREF(Py_None);\r
+    return Py_None;\r
+}\r
+\r
+static PyObject *\r
+random_getrandbits(RandomObject *self, PyObject *args)\r
+{\r
+    int k, i, bytes;\r
+    unsigned long r;\r
+    unsigned char *bytearray;\r
+    PyObject *result;\r
+\r
+    if (!PyArg_ParseTuple(args, "i:getrandbits", &k))\r
+        return NULL;\r
+\r
+    if (k <= 0) {\r
+        PyErr_SetString(PyExc_ValueError,\r
+                        "number of bits must be greater than zero");\r
+        return NULL;\r
+    }\r
+\r
+    bytes = ((k - 1) / 32 + 1) * 4;\r
+    bytearray = (unsigned char *)PyMem_Malloc(bytes);\r
+    if (bytearray == NULL) {\r
+        PyErr_NoMemory();\r
+        return NULL;\r
+    }\r
+\r
+    /* Fill-out whole words, byte-by-byte to avoid endianness issues */\r
+    for (i=0 ; i<bytes ; i+=4, k-=32) {\r
+        r = genrand_int32(self);\r
+        if (k < 32)\r
+            r >>= (32 - k);\r
+        bytearray[i+0] = (unsigned char)r;\r
+        bytearray[i+1] = (unsigned char)(r >> 8);\r
+        bytearray[i+2] = (unsigned char)(r >> 16);\r
+        bytearray[i+3] = (unsigned char)(r >> 24);\r
+    }\r
+\r
+    /* little endian order to match bytearray assignment order */\r
+    result = _PyLong_FromByteArray(bytearray, bytes, 1, 0);\r
+    PyMem_Free(bytearray);\r
+    return result;\r
+}\r
+\r
+static PyObject *\r
+random_new(PyTypeObject *type, PyObject *args, PyObject *kwds)\r
+{\r
+    RandomObject *self;\r
+    PyObject *tmp;\r
+\r
+    if (type == &Random_Type && !_PyArg_NoKeywords("Random()", kwds))\r
+        return NULL;\r
+\r
+    self = (RandomObject *)type->tp_alloc(type, 0);\r
+    if (self == NULL)\r
+        return NULL;\r
+    tmp = random_seed(self, args);\r
+    if (tmp == NULL) {\r
+        Py_DECREF(self);\r
+        return NULL;\r
+    }\r
+    Py_DECREF(tmp);\r
+    return (PyObject *)self;\r
+}\r
+\r
+static PyMethodDef random_methods[] = {\r
+    {"random",          (PyCFunction)random_random,  METH_NOARGS,\r
+        PyDoc_STR("random() -> x in the interval [0, 1).")},\r
+    {"seed",            (PyCFunction)random_seed,  METH_VARARGS,\r
+        PyDoc_STR("seed([n]) -> None.  Defaults to current time.")},\r
+    {"getstate",        (PyCFunction)random_getstate,  METH_NOARGS,\r
+        PyDoc_STR("getstate() -> tuple containing the current state.")},\r
+    {"setstate",          (PyCFunction)random_setstate,  METH_O,\r
+        PyDoc_STR("setstate(state) -> None.  Restores generator state.")},\r
+    {"jumpahead",       (PyCFunction)random_jumpahead,  METH_O,\r
+        PyDoc_STR("jumpahead(int) -> None.  Create new state from "\r
+                  "existing state and integer.")},\r
+    {"getrandbits",     (PyCFunction)random_getrandbits,  METH_VARARGS,\r
+        PyDoc_STR("getrandbits(k) -> x.  Generates a long int with "\r
+                  "k random bits.")},\r
+    {NULL,              NULL}           /* sentinel */\r
+};\r
+\r
+PyDoc_STRVAR(random_doc,\r
+"Random() -> create a random number generator with its own internal state.");\r
+\r
+static PyTypeObject Random_Type = {\r
+    PyVarObject_HEAD_INIT(NULL, 0)\r
+    "_random.Random",                   /*tp_name*/\r
+    sizeof(RandomObject),               /*tp_basicsize*/\r
+    0,                                  /*tp_itemsize*/\r
+    /* methods */\r
+    0,                                  /*tp_dealloc*/\r
+    0,                                  /*tp_print*/\r
+    0,                                  /*tp_getattr*/\r
+    0,                                  /*tp_setattr*/\r
+    0,                                  /*tp_compare*/\r
+    0,                                  /*tp_repr*/\r
+    0,                                  /*tp_as_number*/\r
+    0,                                  /*tp_as_sequence*/\r
+    0,                                  /*tp_as_mapping*/\r
+    0,                                  /*tp_hash*/\r
+    0,                                  /*tp_call*/\r
+    0,                                  /*tp_str*/\r
+    PyObject_GenericGetAttr,            /*tp_getattro*/\r
+    0,                                  /*tp_setattro*/\r
+    0,                                  /*tp_as_buffer*/\r
+    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,           /*tp_flags*/\r
+    random_doc,                         /*tp_doc*/\r
+    0,                                  /*tp_traverse*/\r
+    0,                                  /*tp_clear*/\r
+    0,                                  /*tp_richcompare*/\r
+    0,                                  /*tp_weaklistoffset*/\r
+    0,                                  /*tp_iter*/\r
+    0,                                  /*tp_iternext*/\r
+    random_methods,                     /*tp_methods*/\r
+    0,                                  /*tp_members*/\r
+    0,                                  /*tp_getset*/\r
+    0,                                  /*tp_base*/\r
+    0,                                  /*tp_dict*/\r
+    0,                                  /*tp_descr_get*/\r
+    0,                                  /*tp_descr_set*/\r
+    0,                                  /*tp_dictoffset*/\r
+    0,                                  /*tp_init*/\r
+    0,                                  /*tp_alloc*/\r
+    random_new,                         /*tp_new*/\r
+    _PyObject_Del,                      /*tp_free*/\r
+    0,                                  /*tp_is_gc*/\r
+};\r
+\r
+PyDoc_STRVAR(module_doc,\r
+"Module implements the Mersenne Twister random number generator.");\r
+\r
+PyMODINIT_FUNC\r
+init_random(void)\r
+{\r
+    PyObject *m;\r
+\r
+    if (PyType_Ready(&Random_Type) < 0)\r
+        return;\r
+    m = Py_InitModule3("_random", NULL, module_doc);\r
+    if (m == NULL)\r
+        return;\r
+    Py_INCREF(&Random_Type);\r
+    PyModule_AddObject(m, "Random", (PyObject *)&Random_Type);\r
+}\r