Roulette Betting algorithm

Source: Internet
Author: User
Tags rand

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.