This article mainly introduces the php winning probability algorithm. For more information, see the following code:
$ 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 ');
For more articles about the php winning probability algorithm implementation code, refer to the PHP Chinese website!