#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
int main()
{
int i,j , ansDoor,selectedDoor,openedDoor, oldSelectedDoor;
int result[2]={ 0 };
//plant a seed
srand( time(NULL) );
for( j = 0 ; j < 10 ; j++ )
{
result[0] = result [1] = 0;
for ( i = 0; i < 100000 ; i++ )
{
//random right door
ansDoor = rand() % 4;
// now give a selected door
selectedDoor = rand() % 4;
//let open some wrong door (stupid writing style)
do{
openedDoor = rand() % 4;
}while( openedDoor == ansDoor );
//if we confirm that door.
if( selectedDoor == ansDoor && openedDoor != selectedDoor )
result[0]++;
//if we change random new selected door.
oldSelectedDoor = selectedDoor;
do{
selectedDoor = rand() % 4;
}while( selectedDoor == openedDoor || oldSelectedDoor == selectedDoor );
//collect result
if( selectedDoor == ansDoor )
result[1]++;
}
//print
printf("\n%d\nnochange = %d (%.2f) \nchange = %d (%.2f)\n from %d times",j,
result[0] , ((float)result[0]/i)*100 ,
result[1] , ((float)result[1]/i)*100 , i );
}
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHRpbWUuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgppbnQgbWFpbigpCnsKCWludCBpLGogLCBhbnNEb29yLHNlbGVjdGVkRG9vcixvcGVuZWREb29yLCBvbGRTZWxlY3RlZERvb3I7CglpbnQgcmVzdWx0WzJdPXsgMCB9OwoJCgkvL3BsYW50IGEgc2VlZAoJc3JhbmQoIHRpbWUoTlVMTCkgKTsKCWZvciggaiA9IDAgOyBqIDwgMTAgOyBqKysgKQoJewoJCXJlc3VsdFswXSA9IHJlc3VsdCBbMV0gPSAwOwoJCWZvciAoIGkgPSAwOyBpIDwgMTAwMDAwIDsgaSsrICkKCQl7CgoJCQkvL3JhbmRvbSByaWdodCBkb29yCgkJCWFuc0Rvb3IgPSByYW5kKCkgJSA0OwoJCQoJCQkvLyBub3cgZ2l2ZSBhIHNlbGVjdGVkIGRvb3I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