Advertisement
Guest User

Untitled

a guest
Dec 31st, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. /**
  4. * Created by ECGQoEFX on 3/10/2018.
  5. */
  6. public class Driver {
  7.  
  8. public static void main(String args[]) {
  9. Random random = new Random();
  10. int trials = 1000000;
  11. double totalYoungestBoys = 0;
  12. double totalYoungestGirls = 0;
  13.  
  14. for(int i = 0; i < trials; i++) {
  15. boolean isEldestGirl = true;
  16. boolean isYoungestGirl = random.nextBoolean();
  17.  
  18. //random child opens door
  19. if(random.nextBoolean()) {
  20. //eldest opens door
  21. if(isYoungestGirl) {
  22. totalYoungestGirls += 1;
  23. } else {
  24. totalYoungestBoys += 1;
  25. }
  26.  
  27. } else {
  28. //youngest case
  29. if(!isYoungestGirl) {
  30. //boy opened door, throw out.
  31. } else {
  32. totalYoungestGirls += 1;
  33. }
  34.  
  35. }
  36. }
  37.  
  38. System.out.println("Youngest is girl: " + totalYoungestGirls);
  39. System.out.println("Youngest is boy: " + totalYoungestBoys);
  40.  
  41. System.out.println("Odds of youngest being a boy = " + totalYoungestBoys / (totalYoungestBoys + totalYoungestGirls));
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement