Skip to content

Instantly share code, notes, and snippets.

@vivanov1410
Created May 16, 2013 03:27
Show Gist options
  • Select an option

  • Save vivanov1410/5589166 to your computer and use it in GitHub Desktop.

Select an option

Save vivanov1410/5589166 to your computer and use it in GitHub Desktop.
prize and 3 doors riddle
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NUMBER_OF_GAMES 1000
#define NUMBER_OF_CHOICES 3
int main(int argc, char const *argv[]) {
int arr[NUMBER_OF_GAMES][NUMBER_OF_CHOICES] = {0};
int not_switch_wins = 0;
int switch_wins = 0;
srand(time(NULL));
// init car door
for(int i = 0; i < NUMBER_OF_GAMES; ++i) {
arr[i][rand()%NUMBER_OF_CHOICES] = 1;
}
// init player's choice
for(int i = 0; i < NUMBER_OF_GAMES; ++i) {
int r = rand() % NUMBER_OF_CHOICES;
if(arr[i][r] == 1) {
// if player guessed right
int s = rand() % 2;
if(s == 0) {
// if player switch then he lose
++not_switch_wins;
}
}
else {
// if player guessed wrong
int s = rand() % 2;
if(s == 1) {
// if player switch switch then he win
++switch_wins;
}
}
}
for(int i = 0; i < NUMBER_OF_GAMES; ++i) {
printf("%d%d%d\n", arr[i][0], arr[i][1], arr[i][2]);
}
printf("Total NOT SWITCH wins = %d\nProbability = %f\n", not_switch_wins, (float)not_switch_wins/(float)NUMBER_OF_GAMES * 100.0f);
printf("Total SWITCH wins = %d\nProbability = %f\n", switch_wins, (float)switch_wins/(float)NUMBER_OF_GAMES * 100.0f);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment