If the class A object is known to generate a probability of P (a), the class B object generation probability is P (b), Class C objects,k class objects, their probability sum is 1, how to randomly generate an object in A~k
As the following roulette in the awards
The probability of all prizes and for 1, to turn the roulette will always draw one of the awards, ask once roulette spin produced by the award is which
JavaScript implementation
functionFgetnum () {//all probabilities and for 1,0.15 and 0.150 are to prevent Object.keys from going heavy on key varprobs = {' 0.02 ': ' 0.5% ', ' 0.03 ': ' 0.4% ', ' 0.05 ': ' 0.3% ', ' 0.1 ': ' 50 ', ' 0.15 ': ' 30 ', ' 0.150 ': ' 10 ', ' 0.2 ': ' 5 ', ' 0.3 ': ' 3 '} varres = 0, P=Object.keys (probs), index=Froulette (P); if(Index! =-1) {res=Probs[p[index]]; } returnRes;}//get an index by probabilityfunctionFroulette (p) {varRand =Math.random (); varPointer=0; for(vari=0,len=p.length;i<len;i++) {pointer+=parsefloat (P[i]); if(Rand <=pointer) { returni; } } return-1;}
Reference: http://blog.csdn.net/pymqq/article/details/51375522
Http://www.cnblogs.com/littlebugfish/p/3982593.html
Roulette Betting algorithm