I was curious on how wheel spinning based of chances algorithm would work so I wrote this code
I created an object with each prize and its probability
const chances = {
"Apple" : 22.45,
"Peaches" : 32.8,
"Grapes" : 20,
"Bananas" : 6.58,
"Strawberry" : 18.17
}
then I generate a random number and check if its within the prize's winning ranges
const random = Math.floor((Math.random() * 10000) + 1);
var rangeStart= 0;
for (var key in chances){
var rangeEnd= rangeStart+ chances[key];
if (rangeStart*100 < random && random <= rangeEnd*100){
console.log(rangeStart*100+" < "+random+" <= "+rangeEnd*100);
console.log("You won a "+key)
break;
}
rangeStart+= chances[key];
}
you can check the code here
am I in the right path?