axyd

Poincare Recurrence with Two Jars

Mar 29th, 2018
671
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.83 KB | None | 0 0
  1. #include <stdio.h>          //printf
  2. #include <stdlib.h>         //rand
  3. #include <time.h>           //clock, time
  4. #include <unistd.h>         //fork
  5. #include <sys/types.h>      //clock_t type
  6.  
  7. int draw(const int, const int);
  8.  
  9. int main(){
  10.     srand(time(NULL));  //seed randomizer
  11.  
  12.     int balls= 20;
  13.  
  14.     int urns[balls][2];
  15.  
  16.     //initialize urns
  17.     for (int i= 0; i < balls; ++i){
  18.         urns[i][0]= 1;  //first urn
  19.         urns[i][1]= 0;  //second urn
  20.     }
  21.  
  22.     int ktr1= balls, ktr2= 0, ran= 0, finish= 0;
  23.     long long unsigned int iterCount= 0;
  24.  
  25.     do{
  26.         ran= ( draw(1, balls)) -1;
  27.        
  28.         //visualize urns pre-action
  29.         printf("\nUrn 1: |");
  30.         for(int x=0 ; x<balls; ++x)
  31.             printf("%d|", urns[x][0]);
  32.            
  33.        
  34.         printf("\nUrn 2: |");
  35.         for(int y=0 ; y<balls; ++y)
  36.             printf("%d|", urns[y][1]);
  37.  
  38.         printf("\n-------------------\n");
  39.        
  40.  
  41.         //move ball to urn 2
  42.         if( urns[ran][0] == 1){
  43.             //move ball
  44.             urns[ran][0]= 0;    //first urn
  45.             urns[ran][1]= 1;    //second urn
  46.             //adjust ball counters
  47.             --ktr1;
  48.             ++ktr2;
  49.  
  50.         }
  51.         //move ball to urn 1
  52.         else{
  53.             //move ball
  54.             urns[ran][0]= 1;    //first urn
  55.             urns[ran][1]= 0;    //second urn
  56.             //adjust ball counters
  57.             ++ktr1;
  58.             --ktr2;
  59.         }
  60.  
  61.         //increment iteration counter
  62.         ++iterCount;
  63.        
  64.         //visualize urns
  65. //      printf("\nUrn 1: |");
  66. //      for(int x=0 ; x<balls; ++x)
  67. //          printf("%d|", urns[x][0]);
  68. //         
  69. //     
  70. //      printf("\nUrn 2: |");
  71. //      for(int y=0 ; y<balls; ++y)
  72. //          printf("%d|", urns[y][1]);
  73. //
  74. //      printf("\n-------------------\n");
  75.  
  76.         //end if all balls are in one side
  77.         if(ktr1 == balls || ktr2 == balls) finish= 1;
  78.  
  79.     }while(finish != 1);
  80.  
  81.     printf("\n\n\tFINISHED, iterations: %lld\n\n", iterCount);
  82.  
  83.     _exit(0);
  84. }
  85.  
  86.  
  87. /* Purpose: generates random number from range
  88.  * In : low and upepr bound
  89.  * Out: random number
  90.  */
  91. int draw(const int low, const int high){
  92.  
  93.  
  94.     return (rand() % high + 1);
  95. }
Advertisement
Add Comment
Please, Sign In to add comment