PHP lottery probability of Lottery algorithm program code

Source: Internet
Author: User
Tags foreach time 0

Lottery algorithms need to meet the requirements as follows:

1. Can control the probability of winning

2. Having randomness

3. It is best to control the number of prizes

4. According to user ID or IP, mobile phone number, QQ number and other conditions to limit the number of lottery


Early on these needs, and then according to the information on the Internet, using a phase-type extraction method, we look at the whole of the following procedures:

The program is completed in the framework of the thinkphp, using some of the framework of the class library and functions, I will explain the following, the controller section:

The code is as follows Copy Code
<?php

/**
*
*
* @lanfengye <zibin_5257@163.com>
*/
Class Choujiangaction extends Action {
Start time of the lottery
var $begin _time= "2012-12-25 14:00:00"; Start time 0-No Limit
The end time of the lottery
var $stop _time= "0"; End time 0-No Limit

The lottery prize information, must be in accordance with the order from large to small to fill in, ID for the award, prize for the winning information, V for the probability of winning, Num for the number of prizes
It is important to note that the place must also contain information that does not win the lottery, the probability of sorting from small to large
var $prize _arr = Array (
' 0 ' => array (' ID ' => 1, ' Prize ' => ' 44 yuan purchase 1g/year space ', ' V ' => 1, ' num ' =>1),
' 1 ' => array (' ID ' => 2, ' Prize ' => ' 55 yuan purchase 1g/year space ', ' V ' => 2, ' num ' =>2),
' 2 ' => array (' ID ' => 3, ' Prize ' => ' 66 yuan purchase 1g/year space ', ' V ' => 5, ' num ' =>2),
' 3 ' => array (' ID ' => 4, ' Prize ' => ' 77 yuan purchase 1g/year space ', ' V ' =>, ' num ' =>3),
' 4 ' => array (' ID ' => 5, ' Prize ' => ' 88 yuan purchase 1g/year space ', ' V ' =>, ' num ' =>4),
' 5 ' => array (' ID ' => 6, ' Prize ' => ' 99 yuan purchase 1g/year space ', ' V ' =>, ' num ' =>10),
);


Home Display method
Public Function index () {
Connect the database to get the list of people who won the lottery
$Choujiang =m (' Choujiang ');
$this->assign (' list ', $Choujiang->where ("rid>0")->order (' id desc ')->select ());
Unset ($Choujiang);

Show the start time of the lottery in the home page
$this->assign (' Begin_time ', $this->begin_time);

$this->display ();
}



/**
* Generate winning information, Ajax request this method, customers need to fill in QQ number
*/
Public function make () {
$qq _no= Trim ($_post[' qq_no '));
Import (' ORG. Util.input ');
$qq _no=input::getvar ($qq _no);

if (Empty ($qq _no)) {
$this->ajaxreturn (1, ' Please fill in the QQ number correctly! ');
Exit
}

if (!empty ($this->begin_time) && time () <strtotime ($this->begin_time)) {
$this->ajaxreturn (1, ' Lottery has not yet started, the start time is: '. $this->begin_time);
Exit
}

if (!empty ($this->stop_time) && time () >strtotime ($this->stop_time)) {
$this->ajaxreturn (1, ' This draw is over, the end time is: '. $this->stop_time);
Exit
}

Get an array of award information from private members
$prize _arr= $this->prize_arr;

foreach ($prize _arr as $key => $val) {
$arr [$val [' id ']] = $val [' V '];
}
$rid serial number for winning
$rid = $this->get_rand ($arr); Get the award ID based on probability

$str = $prize _arr[$rid -1][' Prize '); Award in

$Choujiang =m (' Choujiang ');

Get a specific QQ number from the database has already participated in the lottery, if more than or equal to 3 the number of prompts to run out
if ($Choujiang->where ("qq_no= ' {$qq _no} ')->count () >=3) {
$str = ' You have run out of 3 lottery opportunities! ';
$rid = 0;
The number of times a particular award number is obtained from the database, greater than or equal to the maximum number of times the award is drawn, if the last commemorative award is required, modify it
}elseif ($Choujiang->where ("rid={$rid}")->count () >= $prize _arr[$rid ' num ']) {
$str = ' I'm sorry, the award you've drawn has already finished! ';
$rid = 0;
}
Generate a user lottery data to log to the database
$data =array (
' Rid ' => $rid,
' Pop ' => $str,
' Qq_no ' => $qq _no,
' Input_time ' =>time ()
);
Write the user lottery information array to the database

$Choujiang->add ($data);
Unset ($Choujiang);

Ajax return information
$this->ajaxreturn (1, $STR);
}

/**
* Obtain the winning number according to the probability
*/
Private Function Get_rand ($PROARR) {
$result = ';
The total probability precision of probability array
$proSum = Array_sum ($PROARR);
Probability array loop
foreach ($proArr as $key => $proCur) {
$randNum = Mt_rand (1, $proSum);
if ($randNum <= $proCur) {
$result = $key;
Break
} else {
$proSum-= $proCur;
}
}
Unset ($PROARR);
return $result;
}

}



?>

This algorithm is simple to use, concurrent access performance is very good, a little change can be used in various occasions, combined with user login information can effectively control the number of lottery. By changing the start and end to an array, you can refine the process of drawing for a specific time of day.

Related Article

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.