[Python-checkins] python/nondist/sandbox/twister _random.c,1.8,1.9
tim_one@users.sourceforge.net
tim_one@users.sourceforge.net
Sat, 28 Dec 2002 23:30:46 -0800
Update of /cvsroot/python/python/nondist/sandbox/twister
In directory sc8-pr-cvs1:/tmp/cvs-serv28082
Modified Files:
_random.c
Log Message:
Made random_random a more literal copy of the original code's
genrand_res53, and added comments about the obscurities in the method.
Index: _random.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/twister/_random.c,v
retrieving revision 1.8
retrieving revision 1.9
diff -C2 -d -r1.8 -r1.9
*** _random.c 29 Dec 2002 07:15:42 -0000 1.8
--- _random.c 29 Dec 2002 07:30:43 -0000 1.9
***************
*** 126,139 ****
}
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);
}
--- 126,142 ----
}
+ /* random_random is the function named genrand_res53 in the original code;
+ * generates a random number on [0,1) with 53-bit resolution; note that
+ * 9007199254740992 == 2**53; I assume they're spelling "/2**53" as
+ * multiply-by-reciprocal in the (likely vain) hope that the compiler will
+ * optimize the division away at compile-time). 67108864 is 2**26. In
+ * effect, a contains 27 random bits shifted left 26, and b fills in the
+ * lower 26 bits of the 53-bit numerator.
+ */
static PyObject *
random_random(RandomObject *self)
{
! unsigned long a=genrand_int32(self)>>5, b=genrand_int32(self)>>6;
! return PyFloat_FromDouble((a*67108864.0+b)*(1.0/9007199254740992.0));
}