javascript - Get random item for array based on different probabilities? -
okay, imagine i'm creating pokemon game in javascript. have object this...
pokemon = { "pikachu": {hp: 100, probability: 0.1}, "squirtle": {hp: 90, probability: 0.2} };
i need function select pokemon in object @ random, based on probability. so, in case, functions more select "squirtle" it's probability higher "pikachu".
i loop through pokemon array , add of probabilities. call total
then generate value between 0 , total
. call randval
then loop through, adding probabilities again. call secondtotal
first pokemon probability push secondtotal
above randval
selected pokemon.
Comments
Post a Comment