0
|
1 #include "MersenneTwister.h"
|
1
|
2 #include "Python.h"
|
0
|
3
|
|
4 static MTRand mt;
|
|
5
|
|
6 static PyObject *
|
|
7 pymt_seed(PyObject *self, PyObject *args) {
|
|
8 long int seed;
|
|
9
|
|
10 if (!PyArg_ParseTuple(args, "l", &seed))
|
|
11 return NULL;
|
|
12
|
|
13 mt.seed(seed);
|
|
14
|
|
15 Py_INCREF(Py_None);
|
|
16 return Py_None;
|
|
17 }
|
|
18
|
|
19 static PyObject *
|
|
20 pymt_randint(PyObject *self, PyObject *args) {
|
|
21 return PyLong_FromUnsignedLong(mt.randInt());
|
|
22 }
|
|
23
|
|
24 static PyObject *
|
|
25 pymt_random(PyObject *self, PyObject *args) {
|
|
26 return PyFloat_FromDouble(mt.randExc());
|
|
27 }
|
|
28
|
|
29 static PyMethodDef pymtMethods[] = {
|
|
30 {"seed", pymt_seed, METH_VARARGS, "Set seed."},
|
|
31 {"randint", pymt_randint, METH_VARARGS, "Returns random integer."},
|
|
32 {"random", pymt_random, METH_VARARGS, "Returns random number in the interval [0, 1)."},
|
|
33 {NULL, NULL}
|
|
34 };
|
|
35
|
|
36 extern "C" void initpymt() {
|
|
37 (void) Py_InitModule3("pymt", pymtMethods, "Mersenne Twister");
|
|
38 }
|