Skip to content

Instantly share code, notes, and snippets.

@alsotang
Created November 5, 2013 13:36
Show Gist options
  • Save alsotang/7319095 to your computer and use it in GitHub Desktop.
Save alsotang/7319095 to your computer and use it in GitHub Desktop.
男女比例
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