annotate pymt.cpp @ 1:f63d649f91b4 default tip

fix include path
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Mon, 19 May 2008 10:23:11 +0200
parents 0fa810263337
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
1 #include "MersenneTwister.h"
1
f63d649f91b4 fix include path
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
2 #include "Python.h"
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
3
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
4 static MTRand mt;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
6 static PyObject *
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
7 pymt_seed(PyObject *self, PyObject *args) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
8 long int seed;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
9
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
10 if (!PyArg_ParseTuple(args, "l", &seed))
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
11 return NULL;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
12
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
13 mt.seed(seed);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
14
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
15 Py_INCREF(Py_None);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
16 return Py_None;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
17 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
18
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
19 static PyObject *
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
20 pymt_randint(PyObject *self, PyObject *args) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
21 return PyLong_FromUnsignedLong(mt.randInt());
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
22 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
23
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
24 static PyObject *
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
25 pymt_random(PyObject *self, PyObject *args) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
26 return PyFloat_FromDouble(mt.randExc());
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
27 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
28
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
29 static PyMethodDef pymtMethods[] = {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
30 {"seed", pymt_seed, METH_VARARGS, "Set seed."},
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
31 {"randint", pymt_randint, METH_VARARGS, "Returns random integer."},
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
32 {"random", pymt_random, METH_VARARGS, "Returns random number in the interval [0, 1)."},
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
33 {NULL, NULL}
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
34 };
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
35
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
36 extern "C" void initpymt() {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
37 (void) Py_InitModule3("pymt", pymtMethods, "Mersenne Twister");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
38 }

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.