var items = [
{
name: 'X2',
dropChance: 0.33 // Шанс
},
{
name: 'X3',
dropChance: 0.33
},
{
name: 'X5',
dropChance: 0.33
},
{
name: 'X20',
dropChance: 0.33
}
];
function getByChance(items) {
const totalChances = items.reduce((acc, c) => acc + c.dropChance, 0);
const rnd = totalChances * Math.random();
for (let i = 0, sum = 0;; i++) {
sum += items[i].dropChance;
if (sum > rnd) return items[i];
}
}
getByChance(items) // вернёт один из объектов массива