Example of PHP Grand turntable winning probability algorithm

Source: Internet
Author: User
This article mainly introduces the PHP Grand turntable winning probability algorithm. The example describes the implementation process of the probability algorithm, which is of great practical value, for more information about how to implement the PHP Grand turntable winning probability algorithm, see the example in this article. The details are as follows:

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.

This is an APP client with a big turntable lottery algorithm. 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 );

I hope this article will help you with PHP programming.

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.