PHP grand turntable winning probability algorithm and sample code-PHP source code

Source: Internet
Author: User
The big turntable is one of the most interesting items in many online activities recently. Let's take a look at the algorithm and example of the winning probability of the big turntable. I hope it will help you. The big turntable is one of the most interesting items in many online activities recently. Let's take a look at the algorithm and example of the winning probability of the big turntable. I hope it will help you.

Script ec (2); script

Recently, I have a project in my work. The APP client has a big turntable lottery. Of course, we have implemented the lottery on our server. The implementation code is as follows:

The Code is as follows:

Header ("Content-type: text/html; charset = UTF-8 ");
$ Prize_arr = array (
'0' => array ('id' => 1, 'prize' => 'tablet pc', '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 ),
);

$ Actor = 100;

Foreach ($ prize_arr as $ v ){
$ Arr [$ v ['id'] = $ v ['V'];
}
Foreach ($ arr as & $ v ){
$ V = $ v * $ actor;
}
Asort ($ arr );
$ Sum = array_sum ($ arr); // The total probability.

$ Rand = mt_rand (1, $ sum );

$ Result = ''; // The winning product id

Foreach ($ arr as $ k => $ x)
{
If ($ rand <= $ x)
{
$ Result = $ k;
Break;
}
Else
{
$ Rand-= $ x;
}
}
$ Res ['yes'] = $ prize_arr [$ result-1] ['prize']; // medium Award
Print_r ($ res );

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.