fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. class Ideone {
  6.  
  7. static class Child {
  8.  
  9. boolean female;
  10. boolean florida;
  11.  
  12. public Child(boolean female, boolean florida) {
  13. this.female = female;
  14. this.florida = female && florida;
  15. }
  16. }
  17.  
  18. public static void main(String[] args) {
  19. final int FLORIDA_ONE_IN = args.length > 0 ? Integer.parseInt(args[0]) : 10;
  20. Random rnd = new Random();
  21. final Child[][] children = new Child[1000000][];
  22. System.out.println("Generating");
  23. for (int i = 0; i < children.length; i++) {
  24. Child[] pair = new Child[2];
  25. pair[0] = new Child(rnd.nextInt(2) == 0, rnd.nextInt(FLORIDA_ONE_IN) == 0);
  26. pair[1] = new Child(rnd.nextInt(2) == 0, rnd.nextInt(FLORIDA_ONE_IN) == 0);
  27. children[i] = pair;
  28. }
  29. System.out.println("Counting");
  30. int oneIsGirl = 0;
  31. int bothGirls = 0;
  32. int oneIsFlorida = 0;
  33. int oneFloridaBothGirls = 0;
  34. int oneIsFloridaUnique = 0;
  35. int oneFloridaBothGirlsUnique = 0;
  36. for (int i = 0; i < children.length; i++) {
  37. Child[] pair = children[i];
  38. if (pair[0].female || pair[1].female) {
  39. oneIsGirl++;
  40. if (pair[0].female && pair[1].female) {
  41. bothGirls++;
  42. }
  43. }
  44. if (pair[0].florida || pair[1].florida) {
  45. oneIsFlorida++;
  46. oneIsFloridaUnique++;
  47. if (pair[0].female && pair[1].female) {
  48. oneFloridaBothGirls++;
  49. oneFloridaBothGirlsUnique++;
  50. if (pair[0].florida && pair[1].florida) {
  51. // not unique, remove from data
  52. oneIsFloridaUnique--;
  53. oneFloridaBothGirlsUnique--;
  54. }
  55. }
  56. }
  57. }
  58. System.out.printf(">1 girl %7d%n", oneIsGirl);
  59. System.out.printf("both girls %7d%n", bothGirls);
  60. System.out.printf("chance %9.2f%%%n",(100f * bothGirls) / oneIsGirl);
  61. System.out.printf(">1 Florida %7d%n", oneIsFlorida);
  62. System.out.printf("both girls(F) %7d%n", oneFloridaBothGirls);
  63. System.out.printf("chance %9.2f%%%n",(100f * oneFloridaBothGirls) / oneIsFlorida);
  64. System.out.printf(">1 Florida(U) %7d%n", oneIsFloridaUnique);
  65. System.out.printf("both girls(FU) %7d%n", oneFloridaBothGirlsUnique);
  66. System.out.printf("chance %9.2f%%%n",(100f * oneFloridaBothGirlsUnique) / oneIsFloridaUnique);
  67. System.out.println("Captain Hindsight to the rescue??");
  68. int randomIsGirl = 0;
  69. bothGirls = 0;
  70. for (int i = 0; i < children.length; i++) {
  71. Child[] pair = children[i];
  72. int c = rnd.nextInt(2);
  73. if (pair[c].female) {
  74. randomIsGirl++;
  75. if (pair[1-c].female) {
  76. bothGirls++;
  77. }
  78. }
  79. }
  80. System.out.printf("random is girl %7d%n", randomIsGirl);
  81. System.out.printf("both girls %7d%n", bothGirls);
  82. System.out.printf("chance %9.2f%%%n",(100f * bothGirls) / randomIsGirl);
  83. }
  84.  
  85. }
Success #stdin #stdout 0.9s 381632KB
stdin
10
stdout
Generating
Counting
>1 girl         749790
both girls      249827
chance             33.32%
>1 Florida       97921
both girls(F)    47544
chance             48.55%
>1 Florida(U)    95364
both girls(FU)   44987
chance             47.17%
Captain Hindsight to the rescue??
random is girl  499712
both girls      249827
chance             49.99%