The algorithm of winning probability based on PHP code can be used for the lottery algorithm of scraping card, big turntable and so on, _php tutorial

Source: Internet
Author: User
Tags gcd greatest common divisor

Based on the PHP code to achieve the winning probability algorithm can be used for scraping cards, large turntable and other lottery algorithm,


Large turntable winning probability algorithm in our daily life, often encountered, then based on the PHP code is how to achieve the winning probability algorithm, the following through a code example to introduce the probability of PHP winning algorithm, the code is easy to understand, and annotated, the specific code is as follows:

<?php/* * Classic probability algorithm, * $PROARR is a pre-set array, * Assuming the array is: Array (100,200,300,400), * Start is to filter from the probability range of 1,1000 the first number is within the probability range of his occurrence, * if not In, the probability space, that is, the value of K minus the probability space of the first number, * in this case, minus 100, that is, the second number is filtered within the range of 1,900. * This filter to the end, there will always be a number to meet the requirements. * is equivalent to go to a box to touch things, * The first is not, the second is not, the third is not, the last one must be. * This algorithm is simple and very efficient, * The key is that this algorithm has been used in our previous projects, especially the large data volume of the project is very efficient.  */function Get_rand ($PROARR) {$result = ';  The total probability accuracy of the probability array $proSum = Array_sum ($PROARR);   Probability array loop foreach ($proArr as $key + = $proCur) {$randNum = Mt_rand (1, $proSum);    if ($randNum <= $proCur) {$result = $key;   Break   } else {$proSum-= $proCur;  }} unset ($PROARR); return $result; }/* * Awards Array * is a two-dimensional array that records all of the prize information for this draw, * where the ID indicates the winning level, prize represents the prize, and V indicates the probability of winning. * Note that the V must be an integer, and you can set the V of the corresponding prize to 0, meaning that the odds of the prize being pumped are 0, * the sum of V in the array (cardinality), the larger the cardinality, the more accurate the probability. * The sum of V in this example is 100, then the probability of winning the tablet corresponds to 1%, * if the sum of V is 10000, then the probability of winning is one out of 10,000. * */$prize _arr = Array (' 0 ' = = Array (' ID ' =>1, ' prize ' = ' tablet ', ' V ' =>1), ' 1 ' = = Array (' ID ' =>2, ' prize ' =&G t; ' Digital camera ', ' V ' =>5), ' 2 ' => Array (' ID ' =>3, ' prize ' + ' Speaker device ', ' V ' =>10), ' 3 ' = = Array (' ID ' =>4, ' prize ' = ' 4G ') ', ' V ' =>12), ' 4 ' = = Array (' ID ' =>5, ' prize ' = ' 10Q ', ' V ' =>22), ' 5 ' = = Array (' ID ' =>6, ' prize ' = ' = ' next time it will be able to be in oh ', ' V ' = &GT;50),); /* * Each front page request, PHP Loop Award set Array, * Get the prize ID from the Get_rand by the probability calculation function. * Save the winning prizes in the array $res[' yes ', * while the remaining non-winning information is saved in $res[' no ', * finally output JSON data to the front page. */foreach ($prize _arr as $key + $val) {$arr [$val [' id ']] = $val [' V '];} $rid = Get_rand ($arr); Get the prize ID $res by probability [' yes '] = $prize _arr[$rid -1][' Prize '); Medium Prize unset ($prize _arr[$rid-1]); The prize is excluded from the array and the remaining awards are shuffle ($prize _arr); Scrambled array order for ($i =0; $i
 
  

Let's share an example code. Calculation of winning probability based on Java implementation

Do a mobile project, there is a need, to do a wave award activities! One of the winning calculation is disgusting, the user to change the winning probability of each prize, and the number of daily awards are limited. 123456 Prize, the probability is not through, how to calculate a user involved in the not will? Puzzled, you can use the random class of the nextint (int x) method to produce a range of random numbers, resulting in that interval is a few prizes, the winning interval is the generation of dynamic. Post source code, for reference only!

Package Mzone;import Java.util.arraylist;import Java.util.random;public class Mzone {/** * Copyrright (c) 2009-04: * Proje CT: * Module ID: * Comments: Probabilistic calculation * JDK version used:
   
    
 * Author:ch * Create date:2009-04-20 * Modified by: * Modified Date: * Why & What is Modified * version:1.0 * * static random R = new Random ();  public static void Main (string[] args) {//The denominator of the winning probabilities for each prize is integer _5m = new Integer (5);  Integer _500m = new Integer (30);  Integer _ipod = new Integer (500);  Integer _phone = new Integer (1000);  Integer _notebook = new Integer (1500);  Integer _jay = new Integer (50);  ArrayList list = new ArrayList ();  if (_5m.intvalue ()!=0) List.add (_5m);  if (_500m.intvalue ()!=0) List.add (_500m);  if (_ipod.intvalue ()!=0) List.add (_ipod);  if (_phone.intvalue ()!=0) List.add (_phone);  if (_notebook.intvalue ()!=0) List.add (_notebook);  if (_jay.intvalue ()!=0) List.add (_jay);  Computes least common multiple int common = GETN (list);  SYSTEM.OUT.PRINTLN ("Least common Multiple:" +common);  int a = 0;int b = 0;int c = 0;int d = 0;int E = 0;int f = 0;int g = 0;  int first = 0;int Second = 0;int Third = 0;int four = 0;int Fifth = 0;int sixth = 0; if (_5m.intvalue ()!=0) {first = Common/_5M.intvalue ();  } if (_500m.intvalue ()!=0) {second = first + (Common/_500m.intvalue ());  }else second = first;  if (_ipod.intvalue ()!=0) {third = second + (Common/_ipod.intvalue ());  }else third = second;  if (_phone.intvalue ()!=0) {four = third + (Common/_phone.intvalue ());  }else four = third;  if (_notebook.intvalue ()!=0) {fifth = four + (Common/_notebook.intvalue ());  }else fifth = four;  if (_jay.intvalue ()!=0) {sixth = fifth + (Common/_jay.intvalue ());  }else sixth = Fifth; int times = 30000;//cycles for (int i = 0;i < times; i++) {int ri = getrandom (common);//Generate random number if (ri >= 0 &&amp ;   RI < first) {a++;   }else if (ri >= first && ri < second) {b++;   }else if (ri >= second && ri < third) {C + +;   }else if (ri >= third && ri < four) {d++;   }else if (ri >= four && ri < fifth) {e++;   }else if (ri >= fifth && ri < sixth) {f++;   }else{g++; }} System.out.println ("5m value:" + A + "500m value:" + B + "ipodMP3:" + C + "Mobile:" + D + "laptop:" + E + "concert ticket:" + F + "Thank you for participating:" + g);}   /** * Greatest common divisor */public static int gcd (int m, int. N) {while (true) {if ((m = m% n) = = 0) return n;  if ((n = n m) = = 0) return m;  }}/** * least common multiple * * * public static int gys (int z, int y) {int t = 0;  int c = 0;  c = gcd (z,y);  t = z * Y/C; return t;  }/** * For several numbers of least common multiple */public static int getn (ArrayList list) {int t = 1; for (int i = 0;i
    
     
      !--endmain--> You may be interested in the article: 
      
  
  • PHP Implementation can set the winning probability of the Lottery program code sharing
  • large carousel of PHP winning probability algorithm example
  • PHP written Probability algorithm of winning lottery program

http://www.bkjia.com/phpjc/1084593. HTML www.bkjia.com true http://www.bkjia.com/phpjc/1084593.html techarticle the winning probability algorithm based on PHP code can be used to scrape the card, Big turntable and other lottery algorithms, large turntable winning probability algorithm in our daily life, often encountered, then based on the PHP code is ...

  • 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.