Skip to content
Snippets Groups Projects
Commit 58a4eec1 authored by Folkert Nobels's avatar Folkert Nobels
Browse files

First draft of testRandom modified but also with writing to file

parent d207cba7
No related branches found
No related tags found
1 merge request!742Fix the random number generator
...@@ -41,6 +41,8 @@ int main(int argc, char* argv[]) { ...@@ -41,6 +41,8 @@ int main(int argc, char* argv[]) {
message("Seed = %d", seed); message("Seed = %d", seed);
srand(seed); srand(seed);
/* Time-step size */ /* Time-step size */
const int time_bin = 29; const int time_bin = 29;
...@@ -51,10 +53,21 @@ int main(int argc, char* argv[]) { ...@@ -51,10 +53,21 @@ int main(int argc, char* argv[]) {
const integertime_t increment = (1LL << time_bin); const integertime_t increment = (1LL << time_bin);
message("Testing id=%lld time_bin=%d", id, time_bin); message("Testing id=%lld time_bin=%d", id, time_bin);
char buffer[32];
snprintf(buffer, sizeof(char)*32, "fileII%i.txt", i);
FILE *fp;
fp = fopen(buffer,"w");
double total = 0., total2 = 0.; double total = 0., total2 = 0.;
int count = 0; int count = 0;
/* Pearson correlation variables */
double sum_previous_current = 0.;
double previous = 0.;
message("Max nr timesteps = %lld",max_nr_timesteps);
/* Check that the numbers are uniform over the full-range of useful /* Check that the numbers are uniform over the full-range of useful
* time-steps */ * time-steps */
for (integertime_t ti_current = 0LL; ti_current < max_nr_timesteps; for (integertime_t ti_current = 0LL; ti_current < max_nr_timesteps;
...@@ -68,18 +81,30 @@ int main(int argc, char* argv[]) { ...@@ -68,18 +81,30 @@ int main(int argc, char* argv[]) {
total += r; total += r;
total2 += r * r; total2 += r * r;
count++; count++;
const unsigned int test = 127LL*(ti_current - 1LL) + 124429LL;
fprintf(fp, "%f %lld %lld\n", r, (test) % 1514917LL, ti_current );
/* For the pearson correlation */
sum_previous_current += r * previous;
previous = r;
} }
fclose(fp);
const double mean = total / (double)count; const double mean = total / (double)count;
const double var = total2 / (double)count - mean * mean; const double var = total2 / (double)count - mean * mean;
double mean_xy = sum_previous_current / ( (double)count -1.f);
double correlation = (mean_xy-mean*mean)/var;
message("Correlation = %f", correlation);
/* Verify that the mean and variance match the expected values for a uniform /* Verify that the mean and variance match the expected values for a uniform
* distribution */ * distribution */
if ((fabs(mean - 0.5) / 0.5 > 1e-4) || if ((fabs(mean - 0.5) / 0.5 > 2e-4) ||
(fabs(var - 1. / 12.) / (1. / 12.) > 1e-4)) { (fabs(var - 1. / 12.) / (1. / 12.) > 1e-3) ||
(fabs(correlation) > 3e-4)) {
message("Test failed!"); message("Test failed!");
message("Result: count=%d mean=%f var=%f", count, mean, var); message("Result: count=%d mean=%f var=%f, correlation=%f", count, mean, var, correlation);
message("Expected: count=%d mean=%f var=%f", count, 0.5f, 1. / 12.); message("Expected: count=%d mean=%f var=%f, correlation=%f", count, 0.5f, 1. / 12., 0.);
return 1; return 1;
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment