PHP Winning probability algorithm implementation code

Source: Internet
Author: User
Implementation code:

<?php/** *php winning probability algorithm * */function get_zj ($jp, $glname = ' gl ') {    $sum = 0;    foreach ($jp as $k = + $v) {        $sum + = $v [$glname];    }      $R = rand (1, $sum);//Gets the random number      foreach ($jp as $k = $v) {        if ($R <= $v [$glname]) {            return $v;        }        $R = $R-$v [$glname];    }  }    How to use//simulate a winning configuration read from the database  GL for the probability of winning//For example, array (' gl ' = ', ' title ' = ' first prize '); probability of winning =/(10+20+30+40) $JP = Array (); $JP [] = Array (' GL ' = +, ' title ' = ' first prize '); $JP [] = Array (' gl ' = = ', ' title ' = ' Second Prize '); $JP [] = AR Ray (' gl ' = ' + ' title ' = ' third Prize '), $JP [] = Array (' gl ' = ' + ', ' title ' = ' not winning ');    Call the winning probability function//return an array of winning information for example: Array (' gl ' = +, ' title ' = ' first prize '); $ZJ = Get_zj ($JP);  Output array var_dump ($ZJ);

Use method
simulates a winning configuration that is read from a database GL is the probability of winning a prize
For example, the array (' gl ' = ', ' title ' = ' first prize '); probability of winning =/(10+20+30+40)
$j p = Array ();
$jp [] = Array (' GL ' = +, ' title ' = ' first prize ');
$jp [] = Array (' GL ' = +, ' title ' = ' Second Prize ');
$jp [] = Array (' GL ' = +, ' title ' = ' third prize ');
$jp [] = Array (' GL ' = +, ' title ' = ' not winning ');

Call the winning probability function
to return an array of winning information, for example: Array (' gl ' = +, ' title ' = ' first prize ');

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.