Advertisement
desdemona

srandrand

Mar 22nd, 2013
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.18 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4.  
  5. using namespace std;
  6.  
  7. class generatorLin
  8. {
  9. private:
  10.     unsigned   int a,c,m;
  11.     vector <unsigned int> words;
  12.     unsigned  int it;
  13. public:
  14.     generatorLin(unsigned int aa,unsigned int cc,unsigned int mm, unsigned int seed)
  15.     {
  16.         this->a = aa;
  17.         this->c = cc;
  18.         this->m = mm;
  19.         it = 1;
  20.         this->words.push_back(seed);
  21.     }
  22.  
  23.     unsigned int randlin()
  24.     {
  25.         this->words.push_back(0);
  26.         this->words[this->it] = (this->words[this->it-1] * this->a + this->c)%m;
  27.         this->it++;
  28.         return this->words[this->it -1];
  29.     }
  30.  
  31. };
  32.  
  33.  
  34. class zliczacz
  35. {
  36. private:
  37.     unsigned int n,m;
  38.     vector<unsigned int> tab;
  39. public:
  40.     zliczacz(unsigned int nn, unsigned int mm)
  41.     {
  42.         tab.resize(nn);
  43.         for(unsigned int i=0;i<nn;i++)
  44.             tab[i] = 0;
  45.         this->n = nn;
  46.         this->m = mm;
  47.         return ;
  48.     }
  49.  
  50.     void zlicz(unsigned int num)
  51.     {
  52.         tab[num/(this->m/this->n)] ++;
  53.         return ;
  54.     }
  55.  
  56.     void print()
  57.     {
  58.         for(unsigned i =0;i<this->n;i++)
  59.             cout << i* (this->m/n) << " - " << this->tab[i] << endl;
  60.     }
  61.  
  62.  
  63. };
  64.  
  65. class generatorTurbo
  66. {
  67. private:
  68.     vector<bool> words;
  69.     unsigned int p,q;
  70.  
  71. public:
  72.     generatorTurbo()
  73.     {
  74.         words.push_back(0);
  75.         words.push_back(1);
  76.         words.push_back(1);
  77.         words.push_back(1);
  78.         words.push_back(0);
  79.         words.push_back(1);
  80.         words.push_back(1);
  81.         p = 3;
  82.         q = 7;
  83.         return;
  84.     }
  85.  
  86. public:
  87.     unsigned int randTurbo()
  88.     {
  89.         unsigned int a,b;
  90.  
  91.  
  92.         for(int i = 0; i <32; i++)
  93.         {
  94.             a = words[words.size()-p];
  95.             b = words[words.size()-q];
  96.             words.push_back((a+b)%2);
  97.         }
  98.         unsigned int num = 0;
  99.         unsigned int eee = 1;
  100.  
  101.         for(int i = 32; i>0;i--)
  102.         {
  103.             num += words[words.size()-i] * eee;
  104.             eee*=2;
  105.         }
  106.  
  107.         return num;
  108.  
  109.     }
  110.  
  111.  
  112. };
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122. int main()
  123. {
  124.     generatorLin a((int)69069, 1, (int)pow((double)2,(double)31)-1, 15);
  125.     zliczacz b(10, (int)pow((double)2,(double)31)-1);
  126.     for(unsigned int i =0 ; i<100000; i++)
  127.         b.zlicz(a.randlin());
  128.  
  129.     b.print();
  130.    
  131.  
  132.     cout << "========================" << endl;
  133.     unsigned zakres = -1;
  134.  
  135.     generatorTurbo c;
  136.     zliczacz d(10, zakres);
  137.  
  138.     for(unsigned int i =0 ; i<100000; i++)
  139.     {
  140.         if(i%1000 == 0)
  141.             cout << i << endl;
  142.         d.zlicz(c.randTurbo());
  143.     }
  144.     d.print();
  145.    
  146.  
  147.  
  148.     return 0;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement