Php winning probability algorithm implementation code and winning probability algorithm
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 ); // obtain the random number foreach ($ jp as $ k =>v v) {if ($ R <=$ v [$ glname]) {return $ v ;} $ R = $ R-$ v [$ glname] ;}} // usage // simulate a winning configuration read from the database. The probability that gl is the winning configuration. // For example, array ('Gl '=> 10, 'title' => 'first name'); the winning probability is 10/(10 + 20 + 30 + 40) $ jp = array (); $ jp [] = array ('Gl '=> 10, 'title' => 'first name'); $ jp [] = array ('Gl' => 20, 'title' => 'second name'); $ jp [] = array ('Gl '=> 30, 'title' => 'third title '); $ jp [] = array ('Gl '=> 40, 'title' => 'unwon'); // call the winning probability function // return the winning information array, for example: array ('Gl '=> 10, 'title' => 'preemptible'); $ zj = get_zj ($ jp); // output array var_dump ($ zj );
Usage
Simulate the probability that the winning configuration gl is used to win the prize read from the database.
For example, array ('Gl '=> 10, 'title' => 'preemptible'); the probability of winning the prize = 10/(10 + 20 + 30 + 40)
$ Jp = array ();
$ Jp [] = array ('Gl '=> 10, 'title' => 'first prize ');
$ Jp [] = array ('Gl '=> 20, 'title' => 'second prize ');
$ Jp [] = array ('Gl '=> 30, 'title' => 'third prize ');
$ Jp [] = array ('Gl '=> 40, 'title' => 'untouching ');
Call the winning probability function
Returns an array of winning information, for example, array ('Gl '=> 10, 'title' => 'first prize ');