fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <string.h>
  5. int main()
  6. {
  7. int i,j , ansDoor,selectedDoor,openedDoor, oldSelectedDoor;
  8. int result[2]={ 0 };
  9.  
  10. //plant a seed
  11. srand( time(NULL) );
  12. for( j = 0 ; j < 10 ; j++ )
  13. {
  14. result[0] = result [1] = 0;
  15. for ( i = 0; i < 100000 ; i++ )
  16. {
  17.  
  18. //random right door
  19. ansDoor = rand() % 4;
  20.  
  21. // now give a selected door
  22. selectedDoor = rand() % 4;
  23. //let open some wrong door (stupid writing style)
  24. do{
  25. openedDoor = rand() % 4;
  26. }while( openedDoor == ansDoor );
  27. //if we confirm that door.
  28. if( selectedDoor == ansDoor && openedDoor != selectedDoor )
  29. result[0]++;
  30.  
  31. //if we change random new selected door.
  32. oldSelectedDoor = selectedDoor;
  33. do{
  34. selectedDoor = rand() % 4;
  35. }while( selectedDoor == openedDoor || oldSelectedDoor == selectedDoor );
  36.  
  37. //collect result
  38. if( selectedDoor == ansDoor )
  39. result[1]++;
  40.  
  41.  
  42.  
  43. }
  44.  
  45. //print
  46. printf("\n%d\nnochange = %d (%.2f) \nchange = %d (%.2f)\n from %d times",j,
  47. result[0] , ((float)result[0]/i)*100 ,
  48. result[1] , ((float)result[1]/i)*100 , i );
  49. }
  50. return 0;
  51. }
Success #stdin #stdout 0.16s 2680KB
stdin
Standard input is empty
stdout
0
nochange = 24802 (24.80) 
change = 33436 (33.44)
 from 100000 times
1
nochange = 25034 (25.03) 
change = 33417 (33.42)
 from 100000 times
2
nochange = 24913 (24.91) 
change = 33418 (33.42)
 from 100000 times
3
nochange = 24816 (24.82) 
change = 33334 (33.33)
 from 100000 times
4
nochange = 25065 (25.07) 
change = 33425 (33.42)
 from 100000 times
5
nochange = 25114 (25.11) 
change = 33128 (33.13)
 from 100000 times
6
nochange = 25035 (25.04) 
change = 33194 (33.19)
 from 100000 times
7
nochange = 24804 (24.80) 
change = 33695 (33.70)
 from 100000 times
8
nochange = 24972 (24.97) 
change = 33211 (33.21)
 from 100000 times
9
nochange = 25104 (25.10) 
change = 33301 (33.30)
 from 100000 times