/* * 'test-square.c' created: Sat Oct 19 13:37:35 2002 * * Copyright 2002 by Lubomir Host <8host AT pauli.fmph.uniba.sk> * * License: GNU GPL (GNU Public License) * * frekvencny test generatorov nahodnych cisel -- x^2 test dobrej zhody */ /* $Platon$ */ #ifdef HAV_CONFIG_H # include "config.h" #endif #include #include #include #include "main.h" #include "devices.h" #include "tests.h" #include "test-square.h" RNG_test_t RNG_test_square = { "frequency x^2 test of generators", 2E9, test_square }; int test_square(int *counts) { int j = 0, k; int retval = RNG_OK; unsigned int cells; int *n_i; while (rng_devices[j] != NULL) { int i = 0; int cur = 1; int end = counts[0]; Real_t random; Real_t T = 0.0; Real_t n_avg = 0.0; int index = 0; printf("# @@ running test for R.N.G. %s\n", (*rng_devices[j]).desc); printf("# ** \n"); cells = ((unsigned int) (*rng_devices[j]).range >> (*rng_devices[j]).cell_count); n_i = calloc(cells, sizeof(int)); if (n_i == NULL) { fprintf(stderr, "Allocation of %d int's failed!\n", cells); return RNG_FAIL; } /* else { fprintf(stderr, "Allocating of %d int's OK\n", cells); } */ while (counts[i] != -1 && end != -1 && counts[i] <= RNG_test_square.max_count && counts[i] <= (*rng_devices[j]).max_count && retval == RNG_OK) { end = counts[i]; while ((cur - 1) != end && end != -1) { /* printf("cur = %d end = %d\n", cur, end); */ random = (*rng_devices[j]).read(); if ((*rng_devices[j]).cell_count != 0) index = (unsigned int) random >> (*rng_devices[j]).cell_count; else index = (unsigned int) random; /* if (j==1) printf("random = %Lg\trandom_int = %d\tindex = %d\t\n", random, (int) random, index); */ n_i[index]++; /* increment counter for this cell */ /* if (j==1) printf("Incremented n_i[%d] = %d\n", index, n_i[index]); */ cur++; } /* here we are looking for parameter * T = Sum((n_j - n * p_j)^2 / (n * p_j)) = * = (Sum(n_j^2) / (n * p_j) - n) * predpokladame rovnomerne rozdelenie, * p_j = p = konst. */ T = 0.0; n_avg = (1.0 * counts[i]) / cells; for (k = 0; k < cells; k++) { T += (n_i[k] -n_avg) * (n_i[k] -n_avg); /* if (j == 1) printf("%d ", n_i[k]); */ } T = T / n_avg; printf("%d\t%Lg\n", counts[i], T); end = counts[++i + 1]; } j++; /* go to the next device ... */ free(n_i); } return retval; } /* Modeline for ViM {{{ * vim: set ts=4: * vim600: fdm=marker fdl=0 fdc=3: * }}} */