DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Monty Hall Paradox
//Demonstrates the Monty Hall Paradox and theory of probability. (Just some random thing I wanted to do for fun...I assume everything is correct since I got a 2 to 1 ratio?)
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
/**
* @author JT Newsome
*/
public class MontyHallTester {
public static int forMonty = 0;
public static int againstMonty = 0;
public static int min = 0;
public static int max = 2;
public static int timesToLoop = 1000000;
public static void main(String[] args) {
for(int i = 0; i < timesToLoop; i++){
montyTest();
}
System.out.println("forMonty: "+forMonty);
System.out.println("againstMonty: "+againstMonty);
System.out.println("Ratio: "+ (forMonty/againstMonty));
}
public static void montyTest(){
ArrayList<Integer> boxes = new ArrayList<Integer>();
boxes.add(0);
boxes.add(1);
boxes.add(2);
Collections.shuffle(boxes);
Random rand = new Random();
int randomNum1 = rand.nextInt(max - min + 1) + min;
if (boxes.get(randomNum1) == 2){
againstMonty++;
}
else forMonty++;
}
}





