JSFiddle - React, Tailwind, and code Playground
by Gaspar
JavaScript
function weighted_select_without_replacement() {
var weights = {
a_50: -Math.log(Math.random()) / 50,
b_100: -Math.log(Math.random()) / 100,
c_100: -Math.log(Math.random()) / 100
};
var final = [];
// below i just get the 2 smallest
for (var i = 0; i < 2; i++) {
let key = Object.keys(weights).reduce((key, v) => weights[v] < weights[key] ? v : key);
final.push(key);
delete weights[key];
}
return (final);
}
function test_ratios(its) {
var test = [];
for (var i = 0; i < its; i++) {
var news = weighted_select_without_replacement();
test = test.concat(news);
console.log(news.length);
}
window.counts = {};
for (var i2 = 0; i2 < test.length; i2++) {
var num = test[i2];
counts[num] = counts[num] ? counts[num] + 1 : 1;
}
Object.keys(counts).forEach(key => {
counts[key] = Math.round(counts[key] * 100 / its);
});
console.log(counts);
console.log("a/b ratio:", counts.a_50 / counts.b_100);
console.log(counts.a_50 / counts.c_100);
}
test_ratios(1000);
// i'd expect a_50 to have half (50%) the value compared to b_100 and to c_100
// instead, it's about 60%