Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- /**
- * Created by ECGQoEFX on 3/10/2018.
- */
- public class Driver {
- public static void main(String args[]) {
- Random random = new Random();
- int trials = 1000000;
- double totalYoungestBoys = 0;
- double totalYoungestGirls = 0;
- for(int i = 0; i < trials; i++) {
- boolean isEldestGirl = true;
- boolean isYoungestGirl = random.nextBoolean();
- //random child opens door
- if(random.nextBoolean()) {
- //eldest opens door
- if(isYoungestGirl) {
- totalYoungestGirls += 1;
- } else {
- totalYoungestBoys += 1;
- }
- } else {
- //youngest case
- if(!isYoungestGirl) {
- //boy opened door, throw out.
- } else {
- totalYoungestGirls += 1;
- }
- }
- }
- System.out.println("Youngest is girl: " + totalYoungestGirls);
- System.out.println("Youngest is boy: " + totalYoungestBoys);
- System.out.println("Odds of youngest being a boy = " + totalYoungestBoys / (totalYoungestBoys + totalYoungestGirls));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement