[Python-checkins] python/nondist/sandbox/twister MersenneTwister.c,1.2,1.3

rhettinger@users.sourceforge.net rhettinger@users.sourceforge.net
Wed, 18 Dec 2002 11:21:10 -0800


Update of /cvsroot/python/python/nondist/sandbox/twister
In directory sc8-pr-cvs1:/tmp/cvs-serv23557

Modified Files:
	MersenneTwister.c 
Log Message:
Updated comments.
Moved to eight space tabs.


Index: MersenneTwister.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/twister/MersenneTwister.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** MersenneTwister.c	18 Dec 2002 17:57:06 -0000	1.2
--- MersenneTwister.c	18 Dec 2002 19:21:08 -0000	1.3
***************
*** 4,25 ****
  /* ------------------------------------------------------------------
     The code in this module was based on a download from:
!           http://www.math.keio.ac.jp/~matumoto/MT2002/emt19937ar.html
  
     It was modified in 2002 by Raymond Hettinger as follows:
  
! 	* the principal computational lines were untouched.
  
! 	* combined genrand_int32() and genrand_res53() into a 
! 	  single function, random_random().
  
! 	* random_random() and the helper functions, init_genrand() 
! 	  and init_by_array(), were declared static, wrapped in
! 	  Python calling/return code, and had the global data 
! 	  references replaced with structure references.
  
! 	* to simplify code verification, the original code and its
! 	  adjacent wrappers were not converted to Python's preferred
! 	  style for C code.  Instead, the code was left with its
! 	  original indentation and coding conventions.
  
  	* unused functions from the original were deleted.
--- 4,23 ----
  /* ------------------------------------------------------------------
     The code in this module was based on a download from:
! 	  http://www.math.keio.ac.jp/~matumoto/MT2002/emt19937ar.html
  
     It was modified in 2002 by Raymond Hettinger as follows:
  
! 	* the principal computational lines untouched except for tabbing.
  
! 	* renamed genrand_res53() to random_random() and wrapped
! 	  in python calling/return code.
  
! 	* created random_randbelow() for direct access to genrand_int32
! 	  without first creating a float.
  
! 	* genrand_int32() and the helper functions, init_genrand() 
! 	  and init_by_array(), were declared static, wrapped in
! 	  Python calling/return code.  also, their global data 
! 	  references were replaced with structure references.
  
  	* unused functions from the original were deleted.
***************
*** 43,55 ****
  
       1. Redistributions of source code must retain the above copyright
!         notice, this list of conditions and the following disclaimer.
  
       2. Redistributions in binary form must reproduce the above copyright
!         notice, this list of conditions and the following disclaimer in the
!         documentation and/or other materials provided with the distribution.
  
       3. The names of its contributors may not be used to endorse or promote 
!         products derived from this software without specific prior written 
!         permission.
  
     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
--- 41,53 ----
  
       1. Redistributions of source code must retain the above copyright
! 	notice, this list of conditions and the following disclaimer.
  
       2. Redistributions in binary form must reproduce the above copyright
! 	notice, this list of conditions and the following disclaimer in the
! 	documentation and/or other materials provided with the distribution.
  
       3. The names of its contributors may not be used to endorse or promote 
! 	products derived from this software without specific prior written 
! 	permission.
  
     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
***************
*** 74,78 ****
  
  #include "Python.h"
! #include <time.h>		// for seeding to current time
  
  /* Period parameters -- These are all magic.  Don't change. */  
--- 72,76 ----
  
  #include "Python.h"
! #include <time.h>               // for seeding to current time
  
  /* Period parameters -- These are all magic.  Don't change. */  
***************
*** 103,128 ****
  genrand_int32(RandomObject *self)
  {
!     unsigned long y;
!     static unsigned long mag01[2]={0x0UL, MATRIX_A};
!     /* mag01[x] = x * MATRIX_A  for x=0,1 */
!     unsigned long *mt;
  
!     mt = self->state;
!     if (self->index >= N) { /* generate N words at one time */
!         int kk;
  
!         for (kk=0;kk<N-M;kk++) {
!             y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
!             mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL];
!         }
!         for (;kk<N-1;kk++) {
!             y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
!             mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
!         }
!         y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
!         mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];
  
!         self->index = 0;
!     }
    
      y = mt[self->index++];
--- 101,126 ----
  genrand_int32(RandomObject *self)
  {
! 	unsigned long y;
! 	static unsigned long mag01[2]={0x0UL, MATRIX_A};
! 	/* mag01[x] = x * MATRIX_A  for x=0,1 */
! 	unsigned long *mt;
  
! 	mt = self->state;
! 	if (self->index >= N) { /* generate N words at one time */
! 		int kk;
  
! 		for (kk=0;kk<N-M;kk++) {
! 			y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
! 			mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL];
! 		}
! 		for (;kk<N-1;kk++) {
! 			y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
! 			mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
! 	}
! 		y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
! 		mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];
  
! 		self->index = 0;
! 	}
    
      y = mt[self->index++];
***************
*** 137,147 ****
  random_random(RandomObject *self)
  {
!     unsigned long y, z;
!     double result;
  
!     y = genrand_int32(self);
!     z = genrand_int32(self);
!     result = ((y>>5)*67108864.0+(z>>6))*(1.0/9007199254740992.0);
!     return PyFloat_FromDouble(result);
  }
  
--- 135,145 ----
  random_random(RandomObject *self)
  {
! 	unsigned long y, z;
! 	double result;
  
! 	y = genrand_int32(self);
! 	z = genrand_int32(self);
! 	result = ((y>>5)*67108864.0+(z>>6))*(1.0/9007199254740992.0);
! 	return PyFloat_FromDouble(result);
  }
  
***************
*** 149,158 ****
  random_randbelow(RandomObject *self, PyObject *n)
  {
!     unsigned long limit;
  
!     limit = PyInt_AsLong(n);
!     if (limit == -1  && PyErr_Occurred())
  		return NULL;
!     return PyInt_FromLong((long)(genrand_int32(self) % limit));
  }
  
--- 147,156 ----
  random_randbelow(RandomObject *self, PyObject *n)
  {
! 	unsigned long limit;
  
! 	limit = PyInt_AsLong(n);
! 	if (limit == -1  && PyErr_Occurred())
  		return NULL;
! 	return PyInt_FromLong((long)(genrand_int32(self) % limit));
  }
  
***************
*** 161,183 ****
  init_genrand(RandomObject *self, unsigned long s)
  {
!     int mti;
!     unsigned long *mt;
  
!     assert(RandomObject_Check(self));
!     mt = self->state;
!     mt[0]= s & 0xffffffffUL;
!     for (mti=1; mti<N; mti++) {
!         mt[mti] = 
! 	    (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti); 
!         /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
!         /* In the previous versions, MSBs of the seed affect   */
!         /* only MSBs of the array mt[].                        */
!         /* 2002/01/09 modified by Makoto Matsumoto             */
!         mt[mti] &= 0xffffffffUL;
!         /* for >32 bit machines */
!     }
!     self->index = mti;
!     Py_INCREF(Py_None);
!     return Py_None;
  }
  
--- 159,181 ----
  init_genrand(RandomObject *self, unsigned long s)
  {
! 	int mti;
! 	unsigned long *mt;
  
! 	assert(RandomObject_Check(self));
! 	mt = self->state;
! 	mt[0]= s & 0xffffffffUL;
! 	for (mti=1; mti<N; mti++) {
! 		mt[mti] = 
! 		(1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti); 
! 		/* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
! 		/* In the previous versions, MSBs of the seed affect   */
! 		/* only MSBs of the array mt[].                        */
! 		/* 2002/01/09 modified by Makoto Matsumoto             */
! 		mt[mti] &= 0xffffffffUL;
! 		/* for >32 bit machines */
! 	}
! 	self->index = mti;
! 	Py_INCREF(Py_None);
! 	return Py_None;
  }
  
***************
*** 188,213 ****
  init_by_array(RandomObject *self, unsigned long init_key[], unsigned long key_length)
  {
!     unsigned int i, j, k;	/* was signed in the original code. RDH 12/16/2002 */
!     unsigned long *mt;
  
!     mt = self->state;
!     init_genrand(self, 19650218UL);
!     i=1; j=0;
!     k = (N>key_length ? N : key_length);
!     for (; k; k--) {
!         mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL))
!           + init_key[j] + j; /* non linear */
!         mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
!         i++; j++;
!         if (i>=N) { mt[0] = mt[N-1]; i=1; }
!         if (j>=key_length) j=0;
!     }
!     for (k=N-1; k; k--) {
!         mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL))
!           - i; /* non linear */
!         mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
!         i++;
!         if (i>=N) { mt[0] = mt[N-1]; i=1; }
!     }
  
      mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */ 
--- 186,211 ----
  init_by_array(RandomObject *self, unsigned long init_key[], unsigned long key_length)
  {
! 	unsigned int i, j, k;   /* was signed in the original code. RDH 12/16/2002 */
! 	unsigned long *mt;
  
! 	mt = self->state;
! 	init_genrand(self, 19650218UL);
! 	i=1; j=0;
! 	k = (N>key_length ? N : key_length);
! 	for (; k; k--) {
! 		mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL))
! 			 + init_key[j] + j; /* non linear */
! 		mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
! 		i++; j++;
! 		if (i>=N) { mt[0] = mt[N-1]; i=1; }
! 		if (j>=key_length) j=0;
! 	}
! 	for (k=N-1; k; k--) {
! 		mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL))
! 			 - i; /* non linear */
! 		mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
! 		i++;
! 		if (i>=N) { mt[0] = mt[N-1]; i=1; }
! 	}
  
      mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */ 
***************
*** 334,351 ****
  
  static PyMethodDef random_methods[] = {
!         {"random",      (PyCFunction)random_random,  METH_NOARGS,
  		PyDoc_STR("random() -> x in the interval [0,1).")},
!         {"_randbelow",   (PyCFunction)random_randbelow,  METH_O,
  		PyDoc_STR("_randbelow(n) -> integer x in the interval [0,n).")},
!         {"seed",	(PyCFunction)random_seed,  METH_VARARGS,
  		PyDoc_STR("seed(*seeds) -> None.  Defaults to current time")},
!         {"getstate",    (PyCFunction)random_getstate,  METH_NOARGS,
  		PyDoc_STR("getstate() -> tuple containing the current state.")},
!         {"setstate",      (PyCFunction)random_setstate,  METH_O,
  		PyDoc_STR("setstate(state) -> None.  Restores generator state.")},
! 	{"jumpahead",	(PyCFunction)random_jumpahead,  METH_O,
  		PyDoc_STR("jumpahead(int) -> None.  Create a new state from\n\
  the existing state and the supplied integer.")},
! 	{NULL,		NULL}		/* sentinel */
  };
  
--- 332,349 ----
  
  static PyMethodDef random_methods[] = {
! 	{"random",      (PyCFunction)random_random,  METH_NOARGS,
  		PyDoc_STR("random() -> x in the interval [0,1).")},
! 	{"_randbelow",   (PyCFunction)random_randbelow,  METH_O,
  		PyDoc_STR("_randbelow(n) -> integer x in the interval [0,n).")},
! 	{"seed",        (PyCFunction)random_seed,  METH_VARARGS,
  		PyDoc_STR("seed(*seeds) -> None.  Defaults to current time")},
! 	{"getstate",    (PyCFunction)random_getstate,  METH_NOARGS,
  		PyDoc_STR("getstate() -> tuple containing the current state.")},
! 	{"setstate",      (PyCFunction)random_setstate,  METH_O,
  		PyDoc_STR("setstate(state) -> None.  Restores generator state.")},
! 	{"jumpahead",   (PyCFunction)random_jumpahead,  METH_O,
  		PyDoc_STR("jumpahead(int) -> None.  Create a new state from\n\
  the existing state and the supplied integer.")},
! 	{NULL,          NULL}           /* sentinel */
  };
  
***************
*** 355,360 ****
  random_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
  {
!         RandomObject *self;
!         self = (RandomObject *)PyObject_New(RandomObject, &Random_Type);
  	if (self == NULL)
  		return NULL;
--- 353,358 ----
  random_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
  {
! 	RandomObject *self;
! 	self = (RandomObject *)PyObject_New(RandomObject, &Random_Type);
  	if (self == NULL)
  		return NULL;
***************
*** 373,417 ****
  	 * to be portable to Windows without using C++. */
  	PyObject_HEAD_INIT(NULL)
! 	0,				/*ob_size*/
!         "MersenneTwister.Random",	/*tp_name*/
!         sizeof(RandomObject),		/*tp_basicsize*/
! 	0,				/*tp_itemsize*/
  	/* methods */
!         (destructor)PyObject_Del,	/*tp_dealloc*/
! 	0,				/*tp_print*/
!         0,				/*tp_getattr*/
!         0,				/*tp_setattr*/
! 	0,				/*tp_compare*/
! 	0,				/*tp_repr*/
! 	0,				/*tp_as_number*/
! 	0,				/*tp_as_sequence*/
! 	0,				/*tp_as_mapping*/
! 	0,				/*tp_hash*/
!         0,				/*tp_call*/
!         0,				/*tp_str*/
!         PyObject_GenericGetAttr,	/*tp_getattro*/
!         0,				/*tp_setattro*/
!         0,				/*tp_as_buffer*/
!         Py_TPFLAGS_DEFAULT,		/*tp_flags*/
!         random_doc,			/*tp_doc*/
!         0,				/*tp_traverse*/
!         0,				/*tp_clear*/
!         0,				/*tp_richcompare*/
!         0,				/*tp_weaklistoffset*/
!         0,				/*tp_iter*/
!         0,				/*tp_iternext*/
!         random_methods,			/*tp_methods*/
!         0,				/*tp_members*/
!         0,				/*tp_getset*/
!         0,				/*tp_base*/
!         0,				/*tp_dict*/
!         0,				/*tp_descr_get*/
!         0,				/*tp_descr_set*/
!         0,				/*tp_dictoffset*/
!         0,				/*tp_init*/
!         PyType_GenericAlloc,		/*tp_alloc*/
!         random_new,			/*tp_new*/
!         0,				/*tp_free*/
!         0,				/*tp_is_gc*/
  };
  
--- 371,415 ----
  	 * to be portable to Windows without using C++. */
  	PyObject_HEAD_INIT(NULL)
! 	0,                              /*ob_size*/
! 	"MersenneTwister.Random",       /*tp_name*/
! 	sizeof(RandomObject),           /*tp_basicsize*/
! 	0,                              /*tp_itemsize*/
  	/* methods */
! 	(destructor)PyObject_Del,       /*tp_dealloc*/
! 	0,                              /*tp_print*/
! 	0,                              /*tp_getattr*/
! 	0,                              /*tp_setattr*/
! 	0,                              /*tp_compare*/
! 	0,                              /*tp_repr*/
! 	0,                              /*tp_as_number*/
! 	0,                              /*tp_as_sequence*/
! 	0,                              /*tp_as_mapping*/
! 	0,                              /*tp_hash*/
! 	0,                              /*tp_call*/
! 	0,                              /*tp_str*/
! 	PyObject_GenericGetAttr,        /*tp_getattro*/
! 	0,                              /*tp_setattro*/
! 	0,                              /*tp_as_buffer*/
! 	Py_TPFLAGS_DEFAULT,             /*tp_flags*/
! 	random_doc,                     /*tp_doc*/
! 	0,                              /*tp_traverse*/
! 	0,                              /*tp_clear*/
! 	0,                              /*tp_richcompare*/
! 	0,                              /*tp_weaklistoffset*/
! 	0,                              /*tp_iter*/
! 	0,                              /*tp_iternext*/
! 	random_methods,                 /*tp_methods*/
! 	0,                              /*tp_members*/
! 	0,                              /*tp_getset*/
! 	0,                              /*tp_base*/
! 	0,                              /*tp_dict*/
! 	0,                              /*tp_descr_get*/
! 	0,                              /*tp_descr_set*/
! 	0,                              /*tp_dictoffset*/
! 	0,                              /*tp_init*/
! 	PyType_GenericAlloc,            /*tp_alloc*/
! 	random_new,                     /*tp_new*/
! 	0,                              /*tp_free*/
! 	0,                              /*tp_is_gc*/
  };
  
***************
*** 420,426 ****
  
  static PyMethodDef module_functions[] = {
!         {"Random",      (PyCFunction)random_new,  METH_VARARGS,
  		PyDoc_STR("Random(*seeds) -> RandomObject")},
! 	{NULL,		NULL}		/* sentinel */
  };
  
--- 418,424 ----
  
  static PyMethodDef module_functions[] = {
! 	{"Random",      (PyCFunction)random_new,  METH_VARARGS,
  		PyDoc_STR("Random(*seeds) -> RandomObject")},
! 	{NULL,          NULL}           /* sentinel */
  };