4 ms·
I don't think that's true. public class test { public static void main( String[] args ) throws Exception { int boys = 0, girls = 0;
by staktrace 16y ago
I don't think that's true.
public class test {
public static void main( String[] args ) throws Exception {
int boys = 0, girls = 0;
for (int i = 0; i < 1000000; i++) {
boolean isBoy = (Math.random() >= 0.5);
while (! isBoy) {
girls++;
isBoy = (Math.random() >= 0.5);
}
boys++;
}
System.out.println( "Boys: " + boys + "\nGirls: " + girls );
}
}
- spicyj 16y agoI don't know what output you're getting, but this is what I got: $ java test Boys: 1000000 Girls: 996931 $ java test Boys: 1000000 Girls: 1001605 $ java test Boys: 1000000 Girls: 1001656 $ java test Boys: 1000000 Girls: 999473 $ java test Boys: 1000000 Girls: 1000645 Looks pretty even to me. Besides, if you compute the girls/boy ratio mathematically, you get 0/2 + 1/4 + 2/8 + 3/16 + 4/32 + 5/64 + ... which does converge to 1.
- tel 16y agoThis was recently released as an old Google interview question and sparked some controversy. This MathOverflow page [1] is a good (opinionated) summary. The best answer's author believes that with finitely many families the ratio actually doesn't work out evenly, but does converge to 50/50 with infinitely many families. [1] http://mathoverflow.net/questions/17960/google-question-in-a-country-in-which-people-only-want-boys http://mathoverflow.net/questions/17960/google-question-in-a...
- andreyf 16y agoWell, that's pretty obvious. If it involves an infinite sum, but you only have n people, you don't quite get to that infinity-th guy ;)
- tel 16y agoThe answer suggested that even the expected value is never going to be 1/2 but instead something similar to 1/2-1/(4k) for k families which stop having children after the first boy. So, it's a bit more sophisticated than that involving situations where you test the average percentage of an infinite number of islands with k families each.
- klipt 16y agoThat's specifically talking about the expectation of a ratio though. The expectation of the number of girls is always equal to the number of boys, even for finite families. Expectation of a ratio != ratio of expectations.
- anorwell 16y agoMaybe the clearest way to see it is that the expected number of boys is one (obviously--there's always exactly 1), and the expected number of girls is also one (1/2 + 1/4 + ...) since the prob. of (at least) one girl is 1/2, and the prob. of a second girl is 1/4, and so on.
- jackowayed 16y agoI agree. This the first explanation that's made me fully understand and accept this problem.
- staktrace 16y agoHmm. The handful of times I tried it I always got a smaller number for the girls than the boys. Just goes to show sample size is important... :)