diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pymt.cpp	Thu Sep 06 13:58:46 2007 +0200
@@ -0,0 +1,38 @@
+#include "MersenneTwister.h"
+#include "python2.5/Python.h"
+
+static MTRand mt;
+
+static PyObject *
+pymt_seed(PyObject *self, PyObject *args) {
+	long int seed;
+	
+	if (!PyArg_ParseTuple(args, "l", &seed))
+		return NULL;
+	
+	mt.seed(seed);
+	
+	Py_INCREF(Py_None);
+	return Py_None;
+}
+
+static PyObject *
+pymt_randint(PyObject *self, PyObject *args) {
+	return PyLong_FromUnsignedLong(mt.randInt());
+}
+
+static PyObject *
+pymt_random(PyObject *self, PyObject *args) {
+	return PyFloat_FromDouble(mt.randExc());
+}
+
+static PyMethodDef pymtMethods[] = {
+	{"seed",		pymt_seed,		METH_VARARGS, "Set seed."},
+	{"randint",		pymt_randint,	METH_VARARGS, "Returns random integer."},
+	{"random",	pymt_random,	METH_VARARGS, "Returns random number in the interval [0, 1)."},
+	{NULL,				NULL}
+};
+
+extern "C" void initpymt() {
+	(void) Py_InitModule3("pymt", pymtMethods, "Mersenne Twister");
+}

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