Body/girl probability
Answering the question: what is the percentage of boys in a society where couple only stop having babies when they have a boy.
by Alessandro Vernet
HTML
<script src="http://documentcloud.github.com/underscore/underscore.js"></script>
<div>Boys: <span id="boy-count"></span></div>
<div>Girls: <span id="girl-count"></span></div>
<div>Percentage of boys: <span id="boy-percentage"></span></div>
JavaScript
var FAMILIES = 100000;
var boyCount = 0;
var girlCount = 0;
_.each(_.range(FAMILIES), function() {
while(true) {
var isBoy = Math.random() < 0.5;
if (isBoy) boyCount++; else girlCount++;
if (isBoy) break;
}
});
$("#boy-count").text(boyCount);
$("#girl-count").text(girlCount);
$("#boy-percentage").text(boyCount / (boyCount + girlCount) * 100);