Advertisement
double_trouble

GenCheck

Dec 9th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <map>
  6.  
  7. using namespace std;
  8.  
  9. const long long N = 4294967295;
  10. const double a[] = {1.0000886, 0.4713941, 0.0001348028, -0.008553069, 0.00312558, -0.0008426812, 0.00009780499};
  11. const double b[] = {-0.2237368, 0.02607083, 0.01128186, -0.01153761, 0.005169654, 0.00253001, -0.001450117};
  12. const double c[] = {-0.01513904, -0.008986007, 0.02277679, -0.01323293, -0.006950356, 0.001060438, 0.001565326};
  13.  
  14. vector<int> vA;
  15. vector<vector<int> > vB;
  16. vector<long long> vC;
  17. vector<long long> vD(5);
  18. vector<vector<long long> > StirlingNumb;
  19. vector<double> pr;
  20.  
  21. double chiSquared(double alpha, double frdegr)
  22. {
  23.   double res(0);
  24.   double d;
  25.   if (alpha < 0.5) {
  26.     d = -2.0637 * pow(log(1.0 / alpha) - 0.16, 0.4274) + 1.5774;
  27.   }
  28.   else {
  29.     d = 2.0637 * pow(log(1.0 / (1.0 - alpha)) - 0.16, 0.4274) - 1.5774;
  30.   }
  31.  
  32.   for (int j(0); j < 7; ++j) {
  33.     res += pow(frdegr, (-j * 1.0) / 2.0) * pow(d, 1.0 * j) * (a[j] + b[j] / (1.0 * frdegr) + c[j] / (1.0 * frdegr * frdegr));
  34.   }
  35.  
  36.   res = frdegr * pow(res, 3.0);
  37.   return res;
  38. }
  39.  
  40. int checkType(const vector<long long> &vals)
  41. {
  42.   map<long long, int> tp;
  43.   for (int i(0); i < vals.size(); ++i) {
  44.     tp[vals[i]]++;
  45.   }
  46.  
  47.   return tp.size() - 1;
  48. }
  49.  
  50. void countStirling(int d)
  51. {
  52.   StirlingNumb.resize(d + 1);
  53.   for (int i(0); i <= d; ++i) {
  54.     StirlingNumb[i].resize(d + 1);
  55.     StirlingNumb[i][0] = 0;
  56.     StirlingNumb[i][i] = 1;
  57.   }
  58.  
  59.   for (int i(1); i < StirlingNumb.size(); ++i) {
  60.     for (int j(1); j < StirlingNumb[i].size(); ++j) {
  61.       StirlingNumb[i][j] = j * StirlingNumb[i - 1][j] + StirlingNumb[i - 1][j - 1];
  62.     }
  63.   }
  64. }
  65.  
  66. int main()
  67. {
  68.   long long m(4294967296);
  69.   long long a(69069);
  70.   long long c(0);
  71.   long long x0(1);
  72.  
  73.   int d(10);
  74.   vA.resize(d);
  75.  
  76.   long long n;
  77.   cout << "Would you be so kind to enter the amount of numbers <3\n";
  78.   cin >> n;
  79.   int eps;
  80.   double p(1.0 / d);
  81.  
  82.   cout << "\nChechking frequency criterion...\n";
  83.  
  84.   long long x(x0);
  85.   for (int i(0); i < n; ++i) {
  86.     eps = (1ll * d * x) / m;
  87.     vA[eps]++;
  88.     x = (a * x + c) % m;
  89.   }
  90.  
  91.   double V(0);
  92.   for (int i(0); i < d; ++i) {
  93.     V += pow(vA[i] - n * p, 2.0) / (1.0 * n * p);
  94.   }
  95.  
  96.   double chi(chiSquared(0.95, d - 1));
  97.  
  98.   if (V < chi) {
  99.     cout << "Satisfy criterion A\n";
  100.   }
  101.   else {
  102.     cout << "Don't satisfy criterion A\n";
  103.   }
  104.   cout << "V = " << V << "\n";
  105.   cout << "chi-squared(0.95, " << d - 1 << ") = " << chi << "\n";
  106.   //-----------------------------------------------------
  107.   cout << "\nChechking pair criterion...\n";
  108.   vB.resize(d);
  109.   for (int i(0); i < d; ++i) {
  110.     vB[i].resize(d);
  111.   }
  112.  
  113.   x = x0;
  114.   long long aa, bb;
  115.   for (int i(0); i < n / 2; ++i) {
  116.     aa = (d * x) / m;
  117.     x = (a * x + c) % m;
  118.     bb = (d * x) / m;
  119.     vB[aa][bb]++;
  120.   }
  121.  
  122.   p = 1.0 / (d * d);
  123.   V = 0;
  124.   for (int i(0); i < d; ++i) {
  125.     for (int j(0); j < d; ++j) {
  126.       V += pow(vB[i][j] - n / 2 * p, 2.0) / (1.0 * n / 2 * p);
  127.     }
  128.   }
  129.  
  130.   chi = chiSquared(0.95, d * d - 1);
  131.  
  132.   if (V < chi) {
  133.     cout << "Satisfy criterion B\n";
  134.   }
  135.   else {
  136.     cout << "Don't satisfy criterion B\n";
  137.   }
  138.   cout << "V = " << V << "\n";
  139.   cout << "chi-squared(0.95, " << d * d - 1 << ") = " << chi << "\n";
  140.   //------------------------------------------------------------
  141.   cout << "\nChechking series criterion...\n";
  142.   int h = 5;
  143.   vC.resize(h + 1);
  144.   int sercount(0);
  145.  
  146.   x = x0;
  147.   int l(1);
  148.   for (int i(0); i < n; i += 2) {
  149.     aa = 1ll * d * x / m;
  150.     x = (a * x + c) % m;
  151.     bb = 1ll * d * x / m;
  152.     x = (a * x + c) % m;
  153.     if (aa != bb) {
  154.       vC[l - 1]++;
  155.       l = 1;
  156.     }
  157.     else {
  158.       l = min(l + 1, h + 1);
  159.     }
  160.   }
  161.   if (l > 1) {
  162.     vC[l - 1]++;
  163.   }
  164.  
  165.   for (int i(0); i < vC.size(); ++i) {
  166.     sercount += vC[i];
  167.   }
  168.  
  169.   pr.resize(h + 1);
  170.   for (int i(0); i < h; ++i) {
  171.     pr[i] = 9.0 / pow(10.0, 1.0 * (i + 1));
  172.   }
  173.   pr[h] = 1.0 / pow(10.0, 1.0 * h);
  174.  
  175.   V = 0;
  176.   for (int i(0); i <= h; ++i) {
  177.     V += pow(vC[i] - pr[i] * sercount, 2.0) / (1.0 * pr[i] * sercount);
  178.   }
  179.  
  180.   chi = chiSquared(0.95, h);
  181.   if (V < chi) {
  182.     cout << "Satisfy criterion C\n";
  183.   }
  184.   else {
  185.     cout << "Don't satisfy criterion C\n";
  186.   }
  187.   cout << "V = " << V << "\n";
  188.   cout << "chi-squared(0.95, " << h << ") = " << chi << "\n";
  189.   //------------------------------------------------------------
  190.   cout << "\nChechking poker criterion...\n";
  191.   x = x0;
  192.   vector<long long> vals(5);
  193.   int type;
  194.   for (int i(0); i < n; i += 5) {
  195.     int j(0);
  196.     while(j < 5) {
  197.       vals[j] = (d * x) / m;
  198.       x = (a * x + c) % m;
  199.       j++;
  200.     }
  201.     type = checkType(vals);
  202.     vD[type]++;
  203.   }
  204.  
  205.   countStirling(5);
  206.   pr.resize(5);
  207.   for (int k(0); k < 5; ++k) {
  208.     pr[k] = 1;
  209.     int r(k + 1);
  210.     for (int j(1); j <= r; ++j) {
  211.       pr[k] *= (d + 1 - j);
  212.     }
  213.     pr[k] /= pow(d, 5);
  214.     pr[k] *= StirlingNumb[5][r];
  215.   }
  216.  
  217.   V = 0;
  218.   for (int i(0); i < 5; ++i) {
  219.     V += pow(vD[i] - pr[i] * n / 5.0, 2.0) / (1.0 * pr[i] * n / 5.0);
  220.   }
  221.  
  222.   chi = chiSquared(0.95, 4);
  223.  
  224.   if (V < chi) {
  225.     cout << "Satisfy criterion D\n";
  226.   }
  227.   else {
  228.     cout << "Don't satisfy criterion D\n";
  229.   }
  230.   cout << "V = " << V << "\n";
  231.   cout << "chi-squared(0.95, " << 4 << ") = " << chi << "\n";
  232.  
  233.   return 0;
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement