Advertisement
HasanRasulov

Dog-flea.cpp

Nov 6th, 2019 (edited)
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.92 KB | None | 0 0
  1. #include <cstdio>
  2. #include<random>
  3. #include<cmath>
  4. constexpr int gen_n=210;
  5. constexpr int init_a=20;
  6. constexpr int init_b=0;
  7.  
  8. int main() {
  9.  
  10.     int dog_a=init_a,dog_b=init_b;
  11.  
  12.  
  13.     for (int i = 0; i < gen_n; ++i) {
  14.  
  15.         std::random_device rd;
  16.         std::default_random_engine engine(rd());
  17.         std::uniform_int_distribution<int> dist(1,gen_n+1);
  18.  
  19.  
  20.         double pro_a=(double)std::abs(dog_a-dist(engine))/(double)gen_n;
  21.         double pro_b=(double)std::abs(dog_b-dist(engine))/(double)gen_n;
  22.  
  23.          if(pro_a>pro_b&&dog_b>0){
  24.              dog_a++;
  25.              dog_b--;
  26.          }else if(pro_a<pro_b&&dog_a>0){
  27.              dog_a--;
  28.              dog_b++;
  29.          }
  30.  
  31.  
  32.         if(dog_a==init_a&&dog_b==init_b){
  33.             std::printf("Initial State number of generations:%d",i);
  34.             break;
  35.         }
  36.  
  37.  
  38.        std::printf("x(%d)=(%d,%d)\n",i,dog_a,dog_b);
  39.  
  40.     }
  41.  
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement