blob: 38aa45281ccbfc31ae96aee9ef63be0e21b3cbc6 [file] [log] [blame]
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +00001#include "AvalancheTest.h"
2
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +00003//-----------------------------------------------------------------------------
4
5void PrintAvalancheDiagram ( int x, int y, int reps, double scale, int * bins )
6{
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +00007 const char * symbols = ".123456789X";
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +00008
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +00009 for(int i = 0; i < y; i++)
10 {
11 printf("[");
12 for(int j = 0; j < x; j++)
13 {
14 int k = (y - i) -1;
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +000015
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000016 int bin = bins[k + (j*y)];
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +000017
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000018 double b = double(bin) / double(reps);
19 b = fabs(b*2 - 1);
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +000020
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000021 b *= scale;
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +000022
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000023 int s = (int)floor(b*10);
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +000024
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000025 if(s > 10) s = 10;
26 if(s < 0) s = 0;
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +000027
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000028 printf("%c",symbols[s]);
29 }
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +000030
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000031 printf("]\n");
32 }
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +000033}
34
35//----------------------------------------------------------------------------
36
37double maxBias ( std::vector<int> & counts, int reps )
38{
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000039 double worst = 0;
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +000040
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000041 for(int i = 0; i < (int)counts.size(); i++)
42 {
43 double c = double(counts[i]) / double(reps);
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +000044
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000045 double d = fabs(c * 2 - 1);
46
47 if(d > worst)
48 {
49 worst = d;
50 }
51 }
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +000052
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000053 return worst;
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +000054}
55
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +000056//-----------------------------------------------------------------------------