Created
May 18, 2016 00:27
-
-
Save nickcarenza/49d7cb9b02ba6e5729556db699bbf811 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
let chance = require('chance').Chance(); | |
let math = require('mathjs'); | |
let newItem = function() { | |
return { | |
a: chance.natural({min: 1, max: 20}), | |
b: chance.pickone(['alpha', 'bravo', 'charlie', 'delta', 'echo']), | |
c: chance.pickset(['alpha', 'bravo', 'charlie', 'delta', 'echo'], 2), | |
d: chance.natural({min: 1, max: 2}), | |
sanity: 1 | |
} | |
} | |
let list = [] | |
let itemCount = process.argv[2] | |
let setSize = process.argv[3] | |
for (let i = itemCount; i > 0 ; i--) { | |
list.push(newItem()) | |
} | |
// ugly print generated list | |
// process.stdout.write(JSON.stringify(list)+'\n') | |
// pretty print generated list | |
// process.stdout.write(JSON.stringify(list, undefined, 2)) | |
// Conditions in set of list | |
let probabilityOfThingsInSet = (function(list, setSize){ | |
let conditions = [{ | |
filter:function(item){ | |
return item.d == 1 | |
}, | |
count: 1, | |
}] | |
// conditions are ANDed together | |
return conditions.map(function(cond){ | |
return hypergeometricDistribution(cond.count, setSize, list.filter(cond.filter).length, list.length) | |
}).reduce(function(previousValue, currentValue, currentIndex, array) { | |
return previousValue * currentValue | |
}) | |
})(list, setSize) | |
process.stdout.write(JSON.stringify(probabilityOfThingsInSet, undefined, 2)+'\n') | |
// hypergeometric distribution H (X, Y) | |
// H (n) = C (X, n) * C (Y - X, Z - n) / C (Y, Z) | |
// X stands for the number of a certain card that you have in the deck. countInList | |
// Y is the number of cards in the deck. listSize | |
// Z is the number of cards you are drawing. setSize | |
// N is the number you are checking for. countInSet | |
function hypergeometricDistribution(countInSet, setSize, countInList, listSize) { | |
if (countInList < countInSet) { | |
// console.log('countInSet %d, setSize %d, countInList %d, listSize %d', ...arguments) | |
// console.log('countInList %d is less than countInSet %d', countInList, countInSet) | |
return 1 | |
} | |
if (listSize < setSize) { | |
// console.log('countInSet %d, setSize %d, countInList %d, listSize %d', ...arguments) | |
// console.log('list size %d is less than set size %d', listSize, setSize) | |
return 1 | |
} | |
if (listSize-countInList < setSize-countInSet) { | |
// console.log('countInSet %d, setSize %d, countInList %d, listSize %d', ...arguments) | |
// console.log('(listSize-countInList) %d is less than (setSize-countInSet) %d', listSize-countInList, setSize-countInSet) | |
return 0 | |
} | |
return math.combinations(countInList, countInSet) * math.combinations(listSize-countInList, setSize-countInSet) / math.combinations(listSize, setSize) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment