PHP large turntable Winning probability algorithm example _php skill

Source: Internet
Author: User
Tags rand

This article describes the PHP large turntable winning probability algorithm to achieve the method of sharing for everyone to reference. Specifically as follows:

Large turntable is a lot of recent online movement of a more interesting things, let's take a look at this large turntable winning probability algorithm and examples, I hope to help you.

This is an app client with a large turntable lottery algorithm, specifically how the lottery is certainly implemented in our service side. Here's a simple share of the implementation code:

Copy Code code as follows:
Header ("content-type:text/html; Charset=utf-8 ");
$prize _arr = Array (
' 0 ' => array (' ID ' =>1, ' prize ' => ' tablet computer ', ' 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 flash drive ', ' V ' =>12),
' 4 ' => array (' ID ' =>5, ' Prize ' => ' 10Q ', ' V ' =>22),
' 5 ' => array (' ID ' =>6, ' prize ' => ' will be able to be in the next time Oh ', ' 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); Total probability

$rand = Mt_rand (1, $sum);

$result = '; Winning Product ID

foreach ($arr as $k => $x)
{
if ($rand <= $x)
{
$result = $k;
Break
}
Else
{
$rand-= $x;
}
}
$res [' yes '] = $prize _arr[$result -1][' Prize ']; Award in
Print_r ($res);

I hope this article will help you with your PHP program design.

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.