Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h> //printf
- #include <stdlib.h> //rand
- #include <time.h> //clock, time
- #include <unistd.h> //fork
- #include <sys/types.h> //clock_t type
- int draw(const int, const int);
- int main(){
- srand(time(NULL)); //seed randomizer
- int balls= 20;
- int urns[balls][2];
- //initialize urns
- for (int i= 0; i < balls; ++i){
- urns[i][0]= 1; //first urn
- urns[i][1]= 0; //second urn
- }
- int ktr1= balls, ktr2= 0, ran= 0, finish= 0;
- long long unsigned int iterCount= 0;
- do{
- ran= ( draw(1, balls)) -1;
- //visualize urns pre-action
- printf("\nUrn 1: |");
- for(int x=0 ; x<balls; ++x)
- printf("%d|", urns[x][0]);
- printf("\nUrn 2: |");
- for(int y=0 ; y<balls; ++y)
- printf("%d|", urns[y][1]);
- printf("\n-------------------\n");
- //move ball to urn 2
- if( urns[ran][0] == 1){
- //move ball
- urns[ran][0]= 0; //first urn
- urns[ran][1]= 1; //second urn
- //adjust ball counters
- --ktr1;
- ++ktr2;
- }
- //move ball to urn 1
- else{
- //move ball
- urns[ran][0]= 1; //first urn
- urns[ran][1]= 0; //second urn
- //adjust ball counters
- ++ktr1;
- --ktr2;
- }
- //increment iteration counter
- ++iterCount;
- //visualize urns
- // printf("\nUrn 1: |");
- // for(int x=0 ; x<balls; ++x)
- // printf("%d|", urns[x][0]);
- //
- //
- // printf("\nUrn 2: |");
- // for(int y=0 ; y<balls; ++y)
- // printf("%d|", urns[y][1]);
- //
- // printf("\n-------------------\n");
- //end if all balls are in one side
- if(ktr1 == balls || ktr2 == balls) finish= 1;
- }while(finish != 1);
- printf("\n\n\tFINISHED, iterations: %lld\n\n", iterCount);
- _exit(0);
- }
- /* Purpose: generates random number from range
- * In : low and upepr bound
- * Out: random number
- */
- int draw(const int low, const int high){
- return (rand() % high + 1);
- }
Advertisement
Add Comment
Please, Sign In to add comment