rendered paste body// Commandline options: <iterations> <switch>
// iterations is anything greater than 0; switch is 0 for a player that does _not_ switch
// and nonzero for one who does.
#include <stdio.h>
#include <stdlib.h>
int random_gen(float max);
int main(void) {
int money, monty, choice_1, choice_2;
long int wins = 0, losses = 0, trials = 0, i = 0, switch_bool = 0;
float win_rate = 0.0;
scanf("%i %i", &trials, &switch_bool);
if(trials < 0) return(-1);
for(i = 0; i < trials; ++i) {
money = random_gen(3);
choice_1 = random_gen(3);
monty = random_gen(3);
while ((monty == money) || (monty == choice_1))
monty = random_gen(3);
if(switch_bool) {
for(choice_2 = 1; (choice_2 == monty) || (choice_2 == choice_1); ++choice_2);
}
else choice_2 = choice_1;
if(choice_2 == money) ++wins;
else ++losses;
}
if(switch_bool)
printf("Results for player switching doors:\n");
else
printf("Results for player not switching doors:\n");
printf("Of %li trials: %li wins; %li losses; win rate of %f%%\n", trials, wins, losses, (wins*100.0)/trials);
return(0);
}
int random_gen(float max) {
return(1+(int) (max*rand()/(RAND_MAX+1.0)));
}