[{ "name": "Приз №1", "count": 10000 }, { "name": "Приз №2", "count": 400 }, { "name": "Приз №3", "count": 10 }]
const pick = (items) => {
const filteredItems = items.filter(item => item.count > 0);
const total = filteredItems.reduce((acc, item) => acc + item.count, 0);
if (total === 0) {
return null;
}
const pickedChance = Math.random();
let storedChance = 0;
for (const item of filteredItems) {
storedChance += item.count / total;
if (pickedChance < storedChance) {
item.count -= 1;
return item.name;
}
}
}
pick(items);