Mercurial > hg > pymt
comparison pymt.cpp @ 0:0fa810263337
import
| author | Peter Meerwald <pmeerw@cosy.sbg.ac.at> |
|---|---|
| date | Thu, 06 Sep 2007 13:58:46 +0200 |
| parents | |
| children | f63d649f91b4 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:0fa810263337 |
|---|---|
| 1 #include "MersenneTwister.h" | |
| 2 #include "python2.5/Python.h" | |
| 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 } |
