vonkorff
08-06-2008, 10:41 PM
I ran the following code attempting to produce a random integer between 0 and 4 294 967 295 = 256^4 - 1. On my system, a long long is 8 bytes, so there should be plenty of room to hold the value.
I multiplied ran2 by 256^4 - 1. But the number I get by doing this is slightly more frequently even (by 1%) than it is odd! The code below checks the last hex digit of each integer. Can other people reproduce this behavior?
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include "ran.h"
int main() {
int idum = -time(NULL);
int arr[16];
for(int i = 0; i < 16; i++)
arr[i] = 0;
for(long long i = 0; i < 8000000; i++) {
double fraction = ran2(idum);
long long max_Lint = (long long) 256 * (long long) 256 * (long long) 256 * (long long) 256 - 1;
long long Lint = (long long) ((double)max_Lint * fraction);
arr[Lint & 15]++;
}
for(int i = 0; i < 16; i++)
printf("arr[%d] = %d\n", i, arr[i]);
}
I multiplied ran2 by 256^4 - 1. But the number I get by doing this is slightly more frequently even (by 1%) than it is odd! The code below checks the last hex digit of each integer. Can other people reproduce this behavior?
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include "ran.h"
int main() {
int idum = -time(NULL);
int arr[16];
for(int i = 0; i < 16; i++)
arr[i] = 0;
for(long long i = 0; i < 8000000; i++) {
double fraction = ran2(idum);
long long max_Lint = (long long) 256 * (long long) 256 * (long long) 256 * (long long) 256 - 1;
long long Lint = (long long) ((double)max_Lint * fraction);
arr[Lint & 15]++;
}
for(int i = 0; i < 16; i++)
printf("arr[%d] = %d\n", i, arr[i]);
}