Based on PHP code to achieve the winning probability algorithm can be used for scraping cards, large turntable, such as lottery algorithm _php example

Source: Internet
Author: User
Tags gcd php code

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 PHP winning probability algorithm, the code is easy to understand, and with comments, the specific code as follows:

<?php/* Classic probabilistic algorithm, * $PROARR is a pre-set array, * assuming that the array is: Arrays (100,200,300,400), * Starting from 1,1000 this probability range to filter the first number within the probability range of his occurrence,
 * If not, then the probability space, that is, the value of K minus the probability space of just that number, * in this case is minus 100, which means that the second number is filtered in the range 1,900.
 * This filter to the end, there will always be a number to meet the requirements.
 * It's 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 amount of data in the efficiency of the project is very good. 
 * * Function Get_rand ($PROARR) {$result = '; 
 The total probability precision of 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, recording all the lottery award information, * where the ID of the winning rating, prize, said the prize, V to indicate the probability of winning.
 * Note that the V must be an integer, you can set the corresponding award of V to 0, which means that the probability of the award is 0, * the sum of V in the array (radix), the larger the cardinality can reflect the accuracy of the probability.
 * In this case the sum of V is 100, then the tablet computer corresponding to the probability of winning is 1%, * if the sum of V is 10000, the probability of winning is one out of 10,000. * */$prize _arr = Array (' 0 ' => array (' ID ' =>1, ' prize ' => ' tablet computer ', ' V ' =>1 '), ' 1 ' => array (' ID '=>2, ' Prize ' => ' digital camera ', ' V ' =>5 ', ' 2 ' => array (' ID ' =>3, ' Prize ' => ' speaker equipment ', ' V ' =>10 '), ' 3 ' => Array (' Id ' =>4, ' Prize ' => ' 4G USB drive ', ' V ' =>12 ', ' 4 ' => array (' ID ' =>5, ' Prize ' => ' 10Q currency ', ' V ' =>22 '), ' 5 ' => array 
(' id ' =>6, ' Prize ' => ' next time may be able to oh ', ' V ' =>50),);
 * * Each front-end page request, PHP cycle Awards set Array, * through the probability calculation function Get_rand get the award ID in the draw.
 * Save the winning prizes in the array $res[' yes '], * and the remaining outstanding information is kept in the $res[' no '], * Finally output the JSON data to the front-end page. 
*/foreach ($prize _arr as $key => $val) {$arr [$val [' id ']] = $val [' V ']; } $rid = Get_rand ($arr); To obtain the award ID $res [' yes '] = $prize _arr[$rid -1][' Prize '); Award unset ($prize _arr[$rid-1]); Remove the award from the array, leaving the award shuffle ($prize _arr); 
Scrambling array order for ($i =0; $i <count ($prize _arr); $i + +) {$PR [] = $prize _arr[$i] [' Prize ']; 
$res [' no '] = $PR;  Print_r ($res);

Let's share some more examples code based on Java to achieve winning probability calculation

Do mobile projects, there is a need to do a shake award activities! The calculation of the jackpot is disgusting, the user wants to change the winning probability of each award, and the number of prizes per day is limited. 123456 and so on, the probability of not pass, how to calculate a user participation in the middle of it? After a while, you can use the random class of Nextint (int x) method to produce a range of random numbers, generated to that interval is a few awards, the winning interval is the production of dynamic. Post the source code, for reference only!

Package mzone;
Import java.util.ArrayList;
Import Java.util.Random; public class Mzone {/** * Copyrright (c) 2009-04: * Project: * Module ID: * Comments: Probability calculation * JDK version used: ; jdk1.4> * Author:ch * Create date:2009-04-20 * Modified by: * Modified Date: * Why & What is Modified * Ve
 rsion:1.0 */static Random r = new Random ();
  public static void Main (string[] args) {//The denominator Integer of the winning probabilities for each award _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);
  Compute LCM INT common = GETN (list); SysteM.out.println ("LCM:" +common);
  int a = 0;int b = 0;int c = 0;int d = 0;int E = 0;int f = 0;int g = 0;
  INT-A-0;int second = 0;int Third = 0;int four = 0;int Fifth = 0;int sixth = 0;
  if (_5m.intvalue ()!=0) {i = Common/_5m.intvalue ();
  } if (_500m.intvalue ()!=0) {second = a-i + (Common/_500m.intvalue ());
  }else second = A;
  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 < a) {a++;
   }else if (ri >=-i && 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 + "notebook computer:" + E + "concert ticket:" + F + "Thank you
 Xie participates: "+ g";
   /** * GCD/public static int gcd (int m, int n) {while (true) {if ((m = m% n) = 0) return n;
  if ((n = n% m) = = 0) return m;
  }/** * LCM/public static int gys (int z, int y) {int t = 0;
  int c = 0;
  c = gcd (z,y);
  t = z * Y/C;
 return t;
  /** * LCM/public static int getn (ArrayList list) {int t = 1;
   for (int i = 0;i<list.size (); i++) {Integer temp = (integer) list.get (i);
  t = Gys (T,temp.intvalue ()); 
 } return t;
  /** * Generate random number * * public static int getrandom (int y) {int = R.nextint (y);
 return result; }
}

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.