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

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -