commit last state
[ppam-mpi.git] / code / test / Util / t.rng.c
CommitLineData
81923e5c
BA
1#include "lut.h"
2#include "Util/rng.h"
3#include <stdlib.h>
4#include <cds/Vector.h>
5#include <math.h>
6
7// Auxiliary to perform K-S test for a given flag, sample size and bins count
8void aux_ks_test(int flag, uint32_t N, uint32_t nBins)
9{
10 init_rng(flag);
11
12 // Fill the bins
13 uint32_t bins[nBins];
14 for (uint32_t i=0; i<nBins; i++)
15 bins[i] = 0;
16 for (uint32_t i=0; i<N; i++)
17 {
18 Real rf = get_rand_real();
19 uint32_t index = floor(rf*nBins);
20 if (index >= nBins) index = nBins - 1; //in case of...
21 bins[index]++;
22 }
23
24 // Test the bins
25 double ksThreshold = 1.358 / sqrt((double)N);
26 double countPerBin = (double)N / nBins;
27 uint32_t cumulativeSum = 0;
28 for (uint32_t i=0; i<nBins; i++)
29 {
30 cumulativeSum += bins[i];
31 LUT_ASSERT((double)cumulativeSum / N - (i+1)*countPerBin/N < ksThreshold);
32 }
33}
34
35// Kolmogorov-Smirnov test on random real numbers (flag==0)
36void t_rng1()
37{
38 aux_ks_test(0, 1000000, 1000);
39 aux_ks_test(0, 100000, 1000);
40 aux_ks_test(0, 10000, 100);
41}
42
43// Kolmogorov-Smirnov test on random real numbers (flag==1)
44void t_rng2()
45{
46 aux_ks_test(1, 1000000, 1000);
47 aux_ks_test(1, 100000, 1000);
48 aux_ks_test(1, 10000, 100);
49}