Created
November 5, 2013 13:36
-
-
Save alsotang/7319095 to your computer and use it in GitHub Desktop.
男女比例
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _ = require('lodash'); | |
var families = []; | |
for (var i = 0; i < 100000; i++) { | |
var family = []; | |
for (var j = 0; j < 3; j++ ) { | |
// 0 是女孩 1 是男孩 | |
var gender = Math.random() >= 0.5 ? 1 : 0; | |
family.push(gender); | |
} | |
families.push(family); | |
} | |
var thirds = []; | |
families.forEach(function (family) { | |
if (family[0] === 0 && family[1] === 0) {// 前两胎都是女孩 | |
thirds.push(family[2]); | |
} | |
}); | |
var result = _.countBy(thirds, function (gender) { | |
return gender; | |
}); | |
console.log(result); // { '0': 12498, '1': 12585 } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment