Php winning probability algorithm implementation code and winning probability algorithm

Source: Internet
Author: User

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 ');

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.