robin
4/28/2011 1:15:00 AM
"Uno" <merrilljensen@q.com> wrote in message news:8bfh28Fve1U1@mid.individual.net...
| robin wrote:
| > "jacob navia" <jacob@spamsink.net> wrote in message news:i2fir2$op4$1@speranza.aioe.org...
| >
| > | This doesn't work with systems that have unsigned long as a 64 bit quantity.
| > |
| > | I obtain:
| > |
| > | MWC result=3740121002 ?
| > | 4169348530
| > | KISS result=2224631993 ?
| > | 1421918629
| >
| > For a 64-bit machine (using 64-bit integer arithmetic),
| > you'd need to truncate each result to 32 bits. That not
| > only applies to the multiplication, it also applies to addition, etc.
| > On a 32-bit machine, these extra bits are discarded,
| > but in 64-bit arithmetic, they are retained,
| > and unless they are similarly discarded,
| > you won't get the same results.
| > I suggest using IAND(k, 2*2147483647+1)
| > for the truncation.
| >
| > With such modifications in the program,
| > it should then produce the same results on both 32-bit and
| > 64-bit machines.
| >
| > P.S. the product 2*2... is best obtained using ISHFT.
| >
| > | Compiling with 32 bit machine yields:
| > | MWC result=3740121002 ?
| > | 3740121002
| > | KISS result=2224631993 ?
| > | 2224631993
|
| First of all, I think we're talking to the actual George Marsaglia here.
|
| $ gcc -Wall -Wextra geo1.c -o out
| geo1.c: In function 'MWC':
| geo1.c:5: warning: type defaults to 'int' in declaration of 'c'
| geo1.c:5: warning: type defaults to 'int' in declaration of 'j'
| geo1.c:5: warning: unused variable 'i'
| geo1.c: In function 'main':
| geo1.c:21: warning: format '%22u' expects type 'unsigned int', but
| argument 2 has type 'long unsigned int'
| geo1.c:23: warning: format '%22u' expects type 'unsigned int', but
| argument 2 has type 'long unsigned int'
| geo1.c:24: warning: control reaches end of non-void function
|
| $ ./out
| MWC result=3740121002 ?
| 3740121002
| KISS result=2224631993 ?
| 2224631993
| $ cat geo1.c
| static unsigned long xs=521288629,xcng=362436069,Q[4691];
|
| unsigned long MWC(void) /*takes about 4.2 nanosecs or 238 million/
| second*/
| {unsigned long t,x,i; static c=0,j=4691;
| j=(j<4690)? j+1:0;
| x=Q[j];
| t=(x<<13)+c+x; c=(t<x)+(x>>19);
| return (Q[j]=t);
| }
|
| #define CNG ( xcng=69069*xcng+123 )
| #define XS ( xs^=(xs<<13), xs^=(xs>>17), xs^=(xs<<5) )
| #define KISS ( MWC()+CNG+XS ) /*138 million/sec*/
|
| #include <stdio.h>
| int main()
| {unsigned long i,x;
| for(i=0;i<4691;i++) Q[i]=CNG+XS;
| for(i=0;i<1000000000;i++) x=MWC();
| printf(" MWC result=3740121002 ?\n%22u\n",x);
| for(i=0;i<1000000000;i++) x=KISS;
| printf("KISS result=2224631993 ?\n%22u\n",x);
| }
| So, what is all this? In particular, is there something special about
| the value of 3.7 billion?
No, nothing special at all.
The purpose of the exercise is just to confirm that after generating
1000000000 random numbers, you get the same answer as George does.