Skip to content

Instantly share code, notes, and snippets.

@MasterAlish
Created April 19, 2017 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MasterAlish/c559abe1407d69fefb3dfe14a3f238bb to your computer and use it in GitHub Desktop.
Save MasterAlish/c559abe1407d69fefb3dfe14a3f238bb to your computer and use it in GitHub Desktop.
import java.util.*;
/**
* Created by master Alish on 4/19/17.
*/
public class WillYouChangeYourChoice {
private static final int TRIES = 1000;
private static final int VARS = 3;
private static boolean[] choices = new boolean[VARS];
private static int myChoice;
private static int totallyWrongChoiceThatIsNotChosenByMe;
private static Random rnd = new Random();
public static void main(String[] args) {
checkWhenIDoNotChangeMyChoice();
checkWhenIChangeMyChoice();
}
private static void checkWhenIDoNotChangeMyChoice() {
int iWinCounts = 0;
for(int i=0; i<TRIES; i++) {
choices = generateRandomVariants();
myChoice = chooseRandomVariant();
totallyWrongChoiceThatIsNotChosenByMe = getTotallyWrongChoiceThatIsNotChosenByMe(myChoice, choices);
myChoice = myChoice;
if (choices[myChoice]){
iWinCounts ++;
}
}
System.out.println(String.format("I win %d times of %d tries when I did not change my choice", iWinCounts, TRIES));
}
private static void checkWhenIChangeMyChoice() {
int iWinCounts = 0;
for(int i=0; i<TRIES; i++) {
choices = generateRandomVariants();
myChoice = chooseRandomVariant();
totallyWrongChoiceThatIsNotChosenByMe = getTotallyWrongChoiceThatIsNotChosenByMe(myChoice, choices);
myChoice = changeChoice(myChoice, totallyWrongChoiceThatIsNotChosenByMe);
if (choices[myChoice]){
iWinCounts ++;
}
}
System.out.println(String.format("I win %d times of %d tries when I change my choice", iWinCounts, TRIES));
}
private static int changeChoice(int myChoice, int totallyWrongChoiceThatIsNotChosenByMe) {
Set<Integer> allChoices = new HashSet<>();
allChoices.add(0);
allChoices.add(1);
allChoices.add(2);
allChoices.remove(myChoice); // not my choice
allChoices.remove(totallyWrongChoiceThatIsNotChosenByMe); //not wrong choice
return (int) allChoices.toArray()[0]; // last choice
}
private static boolean[] generateRandomVariants() {
boolean[] vars = new boolean[VARS];
vars[rnd.nextInt(VARS)] = true;
return vars;
}
private static int chooseRandomVariant() {
return rnd.nextInt(VARS);
}
private static int getTotallyWrongChoiceThatIsNotChosenByMe(int myChoice, boolean[] choices) {
for(int i=0;i<choices.length; i++){
if(choices[i] == false && myChoice != i){
return i;
}
}
throw new RuntimeException("Something went wrong");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment