The winning probability algorithm based on PHP code can be used for lottery algorithms such as scratch cards and big turntable _ php instances

Source: Internet
Author: User
The big turntable winning probability algorithm is often used in our daily lives. How to Implement the winning probability algorithm based on the php code, next we will use a code example to introduce you to the php winning probability algorithm grand turntable winning probability algorithm in our daily life, which we often encounter. How can we implement the winning probability algorithm based on the php code, the following code is used to introduce the php winning probability algorithm. The code is easy to understand and annotated. The Code is as follows:

<? Php/** classic probability algorithm. * $ proArr is a preset array. * assume that the array is array (100,200,300,400 ), * The start is to check whether the first number is within the probability range of 1,. * If not, the probability space is used, that is, the value of k minus the probability space of the number just now. * In this example, we subtract 100, that is, the second number is filtered within the range of 1,900. * In this way, there will always be a number that meet the requirements. * It is equivalent to touching something in a box. * The first is not, the second is not, and the third is not. The last one must be. * This algorithm is simple and highly efficient. * The key is that it has been used in our previous projects, especially in projects with large data volumes. */Function get_rand ($ proArr) {$ result = ''; // The total probability precision 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 ;} /** award array * is a two-dimensional array that records all prize information for this lottery. * The id indicates the winning level, the prize indicates the prize, and the v indicates the winning probability. * Note that the value of v must be an integer. You can set the value of v for the corresponding award to 0, which means that the percentage of the award is 0, * The sum (base number) of values in the array. The larger the base number, the more accurate the probability is. * In this example, the sum of v is 100, and the probability of winning a tablet is 1%. * If the sum of v is 10000, the probability of winning a tablet is one in ten. **/$ Prize_arr = array ('0' => array ('id' => 1, 'prize' => 'tablet ', 'V' => 1 ), '1' => array ('id' => 2, 'prize' => 'digital camera ', 'V' => 5 ), '2' => array ('id' => 3, 'prize' => 'speaker device', 'V' => 10 ), '3' => array ('id' => 4, 'prize' => '4g disks', 'V' => 12 ), '4' => array ('id' => 5, 'prize' => '10q coin ', 'V' => 22 ), '5' => array ('id' => 6, 'prize' => 'next time you may be able to get in,', 'V' => 50 ),); /** for each request on the front-end page, PHP loop awards are set as an array. * The probability calculation function get_rand is used to obtain the prize id. * Save the prize to the array $ res ['yes'], and * Save the remaining unwon information in $ res ['no, * output the json data count to the front-end page. */Foreach ($ prize_arr as $ key => $ val) {$ arr [$ val ['id'] = $ val ['V'];} $ rid = get_rand ($ arr); // obtain the award id based on the probability $ res ['yes'] = $ prize_arr [$ rid-1] ['prize']; // medium award unset ($ prize_arr [$ rid-1]); // removes the medium Award from the array, and the remaining unmiddle award shuffle ($ prize_arr ); // disrupt the array order for ($ I = 0; $ I
 
  

The following is an example of how to calculate the winning probability based on Java.

Do a mobile project and have a requirement. Do a lottery activity! The winning calculation is disgusting. users need to change the winning probability of each award, and there is a limit on the number of awards each day. The fifth and sixth prizes in 1234 are not likely to pass. How can I calculate a user's participation in the mid-finals? After a hard time, you can use the nextInt (int x) method of the Random class to generate a Random number in the range. The Random number generated in that range is a few prizes, and the winning range is dynamic. Paste the source code for your 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:
   
    
* Author: ch * Create Date: * Modified By: * Modified Date: * Why & What is modified * Version: 1.0 */static Random r = new Random (); public static void main (String [] args) {// denominator Integer _ 5 m = new Integer (5) of the probability of winning each award ); integer _ 500 m = 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 (_ 5 m); if (_ 500m. intValue ()! = 0) list. add (_ 500 m); 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); // calculate the minimum public multiple int common = getN (list); System. out. println ("least common:" + 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 second th = 0; int sixth = 0; if (_ 5 m. 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) {th = four + (common/_ notebook. intValue ();} else th = four; if (_ jay. intValue ()! = 0) {sixth = TH + (common/_ jay. intValue ();} else sixth = th; int times = 30000; // number of cycles for (int I = 0; I <times; I ++) {int ri = getRandom (common); // generates a random number. if (ri> = 0 & 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 <th) {e ++ ;} else if (ri >=th & ri <sixth) {f ++;} else {g ++;} System. out. println ("5 m value:" + a + "m value:" + B + "ipodMP3:" + c + "Mobile Phone:" + d + "LAPTOP: "+ e +" concert tickets: "+ f +" Thank you for your participation: "+ g);}/*** calculate the maximum Approximate Number */public static int gcd (int m, int n) {while (true) {if (m = m % n) = 0) return n; if (n = n % m) = 0) return m ;}}/*** returns the smallest public 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 ;} /*** calculate the minimum public multiple of several numbers */public static int getN (ArrayList list) {int t = 1; for (int I = 0; I
    
   

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.