const
getIndex = (prob) => prob.findIndex((r => p => r < p || (r -= p, false))(Math.random())),
normalized = array => {
var sum = array.reduce((a, b) => a + b, 0);
return array.map(v => v / sum);
};
var items = ['a', 'b', 'c', 'd'],
probabilities = [0.6, 0.2, 0.1, 0.1],
counts = { a: { 0: 0, 1: 0, 2: 0, 3: 0 }, b: { 0: 0, 1: 0, 2: 0, 3: 0 }, c: { 0: 0, 1: 0, 2: 0, 3: 0 }, d: { 0: 0, 1: 0, 2: 0, 3: 0 } },
l = 100,
index,
result = [],
subP,
subI,
temp;
while (l--) {
temp = [];
subP = probabilities.slice();
subI = items.slice();
while (subP.length) {
sum = subP.reduce
index = getIndex(normalized(subP));
temp.push(subI[index]);
subI.splice(index, 1);
subP.splice(index, 1);
}
result.push(temp);
}
console.log(result.map(a => a.join()));
result.forEach(a => a.forEach((v, i) => counts[v][i]++));
console.log(counts);
.as-console-wrapper { max-height: 100% !important; top: 0; }
If you wish to learn more about how to use python for data science, then go through data science python programming course by Intellipaat for more insights.