I have this:
var users = [user1, user2, user3];
var chances = [20, 20, 60];
var total = 100;
var random = Math.floor((Math.random() * total) + 1);
if (random > 40 && random < 100) {
console.log('winner:', users[2]);
}
if (random > 20 && random < 40) {
console.log('winner:', users[1]);
}
if (random > 0 && random < 20) {
console.log('winner:', users[0]);
}
This will give every user a fair chance to win. (60 has a 60% chance, 20 has a 20% chance).
But what I actually need is that this is a dynamic for each (or anything else) function.
Example of my thoughts:
chances.forEach(function(entry) {
if (unknownValue > unkownValue2 && unknownValue3 < unknownValue4) {
console.log('winner:', unknownUser);
};
});
So basically, if the value for the chances array are 50, 100 and 20 the chance that number 100 wins must be 2x higher than 50, and 5x higher than 20.
Im happy for every answer and please do not mark this as duplicate for slot machines percentages, this is NOT what i need.