fork download
  1. import java.util.Random;
  2.  
  3. public class Main{
  4. public static void main(String[] args){
  5.  
  6. Random rand = new Random(); //fair random number generator
  7. int victories = 0, trials = 0; //victories is how often we guess correctly; trials is how often we try
  8. for(int i = 0; i < 10000; i++){ //try 10000 times
  9. //first, pick a card
  10. int card = rand.nextInt(3);//0 = red/red; 1 = red/blue; 2 = blue/blue
  11. //then pick a side
  12. int side = rand.nextInt(2);//0 = first; 1 = second.
  13.  
  14. //variable for whether or not the top is red (initialized to false, but it'll get changed later)
  15. boolean seenRed = false;
  16. //variable for whether or not the bottom is red
  17. boolean hiddenRed = false;
  18. //variable for whether or not we predict that the bottom is red
  19. boolean predictedRed = false;
  20.  
  21. //this chunk determines the value of the above three booleans
  22. switch(card){
  23. case 0://It's red/red. We see red, predict red, and red is hidden
  24. seenRed = false;
  25. predictedRed = true;
  26. hiddenRed = true;
  27. break;
  28. case 1: //it's red/blue
  29. if(side == 0){
  30. seenRed = true;
  31. predictedRed = true;
  32. hiddenRed = false;
  33. }
  34. else{
  35. seenRed = false;
  36. predictedRed = false;
  37. hiddenRed = true;
  38. }
  39. break;
  40. case 2: //it's blue/blue
  41. seenRed = false;
  42. predictedRed = false;
  43. hiddenRed = false;
  44. break;
  45. }
  46. //Check to see if we guessed right
  47. if(predictedRed == hiddenRed){
  48. victories++;
  49. }
  50. //either way, note the trial
  51. trials++;
  52. }
  53. System.out.println(victories + " " + trials);
  54. }
  55. }
Success #stdin #stdout 0.1s 320256KB
stdin
Standard input is empty
stdout
6692 10000