validating the selector

node v14.20.1
version: 0.0.0
endpointsharetweet
const donations = { droo: 10, notDroo: 5, someOtherNotDroo: 5, }; const randomList = Object.entries(donations) .map(([name, value]) => ({ name, value: Math.floor(value / 5) })) .sort(() => 0.5 - Math.random()); const getSelection = () => { const maxDonations = randomList.reduce((sum, thing) => sum + thing.value, 0); const randomValueSelection = maxDonations * Math.random(); let sum = 0; return randomList.find(e => { sum += e.value; if (sum > randomValueSelection) return true; }); } const assurances = () => { let accumulator = {}; // accumulate the results of 5 million runs for (let _i = 0; _i < 5000000; _i++) { const selection = getSelection(); if (accumulator[selection.name]) { accumulator[selection.name]++; } else { accumulator[selection.name] = 1; } } const maxHit = Object.values(accumulator).reduce((summa, val) => summa + val, 0); return Object.entries(accumulator).reduce((acc, [name, value]) => ({ ...acc, [name]: `${(100 * value / maxHit).toFixed(3)}%` }),{}) } assurances();
Loading…

no comments

    sign in to comment