inherit
97216
0
Nov 23, 2024 12:51:52 GMT -8
Bennett 🚀
Formerly iPokemon.
3,622
January 2007
catattack
iPokemon's Mini-Profile
|
Post by Bennett 🚀 on Sept 20, 2014 8:50:18 GMT -8
function randomize(objects){
var objTotalWeight = 0, objCWeight = 0, i;
for(i = 0; i < objects.length; i++){
objTotalWeight += objects[i][1];
}
var random = Math.floor(Math.random() * objTotalWeight);
for(i = 0; i < objects.length; i++){
objCWeight += objects[i][1];
if(random < objCWeight){
return objects[i][0];
}
}
} To use: var pokemon = [
["Pikachu", 70], //70% chance
["Raichu", 20], //20% chance
["Pichu", 10] //10% chance
]; var result = randomize(pokemon); For best results, weights should add up to 100. jsfiddle.net/jhyLs2pr/2/
|
|