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

rhettinger@users.sourceforge.net rhettinger@users.sourceforge.net
Wed, 18 Dec 2002 09:57:09 -0800


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

Modified Files:
	MersenneTwister.c 
Log Message:
Removed randomlist() method from interface.
Added _randbelow(n) method for fast integer generation.


Index: MersenneTwister.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/twister/MersenneTwister.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** MersenneTwister.c	17 Dec 2002 00:22:46 -0000	1.1
--- MersenneTwister.c	18 Dec 2002 17:57:06 -0000	1.2
***************
*** 98,109 ****
  /* Random methods */
  
! static PyObject *
! random_random(RandomObject *self)
  {
!     unsigned long y, z;
      static unsigned long mag01[2]={0x0UL, MATRIX_A};
      /* mag01[x] = x * MATRIX_A  for x=0,1 */
      unsigned long *mt;
-     double result;
  
      mt = self->state;
--- 98,110 ----
  /* Random methods */
  
! 
! /* generates a random number on [0,0xffffffff]-interval */
! static unsigned long 
! 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;
***************
*** 130,144 ****
      y ^= (y << 15) & 0xefc60000UL;
      y ^= (y >> 18);
  
!     assert(N%2 == 0 && self->index < N);
!     z = mt[self->index++];   /* doesn't range check.  relies on N being even */
!     z ^= (z >> 11);
!     z ^= (z << 7) & 0x9d2c5680UL;
!     z ^= (z << 15) & 0xefc60000UL;
!     z ^= (z >> 18);
      result = ((y>>5)*67108864.0+(z>>6))*(1.0/9007199254740992.0);
      return PyFloat_FromDouble(result);
  }
  
  /* initializes mt[N] with a seed */
  static PyObject * 
--- 131,160 ----
      y ^= (y << 15) & 0xefc60000UL;
      y ^= (y >> 18);
+     return y;
+ }
  
! static PyObject *
! 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);
  }
  
+ static PyObject *
+ 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));
+ }
+ 
  /* initializes mt[N] with a seed */
  static PyObject * 
***************
*** 317,340 ****
  }
  
- static PyObject *   // XXX ? make an iterator  ? remove from interface when testing is done
- random_randomlist(RandomObject *self, PyObject *cnt)
- {
- 	long count, i;
- 	PyObject *list;
- 
- 	count = PyInt_AsLong(cnt);
- 	if (count == -1  && PyErr_Occurred())
- 		return NULL;
- 	list = PyList_New((int) count);
- 	if (list == NULL)
- 		return NULL;
- 	for (i=0 ; i<count ; i++)
- 		PyList_SET_ITEM(list, i, random_random(self));
- 	return list;
- }
- 
  static PyMethodDef random_methods[] = {
          {"random",      (PyCFunction)random_random,  METH_NOARGS,
  		PyDoc_STR("random() -> x in the interval [0,1).")},
          {"seed",	(PyCFunction)random_seed,  METH_VARARGS,
  		PyDoc_STR("seed(*seeds) -> None.  Defaults to current time")},
--- 333,341 ----
  }
  
  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")},
***************
*** 346,351 ****
  		PyDoc_STR("jumpahead(int) -> None.  Create a new state from\n\
  the existing state and the supplied integer.")},
- 	{"randomlist",	(PyCFunction)random_randomlist,  METH_O,
- 		PyDoc_STR("randomlist(len) -> list.  Return a list of random floats.")},
  	{NULL,		NULL}		/* sentinel */
  };
--- 347,350 ----