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

Popular posts from this blog

Eclipse formatter for java ending braces -

ruby - Nesting modules inside of a Rails eninge gem -

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -