Php random lottery instance analysis

Source: Internet
Author: User
This article mainly introduces the php random lottery implementation method. The example analyzes the php lottery lottery_tool and Its Usage skills. For more information, see

This article mainly introduces the php random lottery implementation method. The example analyzes the php lottery lottery_tool and Its Usage skills. For more information, see

This example describes the usage of php random lottery. Share it with you for your reference. The specific analysis is as follows:

1. Get the random lottery result based on the set probability.

The Code is as follows:

<? Php
/**
* Lottery Tool
*/
Class lottery_tool {
Protected static $ awardsArr;
Protected static $ proField = 'bability ';
Protected static $ proSum = 0;
Protected static $ checkAward = false;
Const SUCCESS_CODE = 0;
Const FAIL_CODE =-1;
// Check the lottery data
Protected static function checkAwards (){
If (! Is_array (self ::$ awardsArr) | empty (self ::$ awardsArr )){
Return self: $ checkAward = false;
}
Self: $ proSum = 0;
Foreach (self ::$ awardsArr as $ _ key => $ award ){
Self: $ proSum + = $ award [self: $ proField];
}
If (empty (self: $ proSum )){
Return self: $ checkAward = false;
}
Return self: $ checkAward = true;
}
Protected static function successRoll ($ rollKey ){
Return array ('code' => self: SUCCESS_CODE, 'roll _ key' => $ rollKey, 'msg '=> 'roll success ');
}
Protected static function failRoll ($ msg = 'roll fail '){
Return array ('code' => self: FAIL_CODE, 'msg '=> $ msg );
}
// Lottery
Public static function roll (){
If (false = self: $ checkAward ){
Return self: failRoll ('awards data is not the right format! ');
}
$ Result = mt_rand (0, self: $ proSum );
$ ProValue = 0;
Foreach (self ::$ awardsArr as $ _ key => $ value ){
$ ProValue + = $ value [self: $ proField];
If ($ result <= $ proValue ){
Return self: successRoll ($ _ key );
}
}
Return self: failRoll ('wrong ');
}
// Change the probability field name
Public static function setProField ($ field = null ){
If (! Empty ($ field )){
Self: $ proField = $ field;
}
}
// Set Prize
Public static function setAwards ($ awards ){
Self: $ awardsArr = $ awards;
Self: checkAwards ();
}
}


2. Sample Code:

The Code is as follows:

$ Awards = array (
'0' => array ('pro' => 15, 'info' => '15% likelihood '),
'1' => array ('pro' => 25, 'info' => '25% likelihood '),
'2' => array ('pro' => 40, 'info' => '40% likelihood '),
'3' => array ('pro' => 20, 'info' => '20% likelihood '),
);
Lottery_tool: setProField ('pro ');
Lottery_tool: setAwards ($ awards );
$ Result = array ();
For ($ I = 10000; $ I --;){
$ Result [] = lottery_tool: roll ();
}
Foreach ($ result as $ key => $ value ){
$ Awards [$ value ['roll _ key'] ['num'] ++;
}
Echo'

';
Var_dump ($ awards );

The running result is as follows:

The Code is as follows:

Array
0 =>
Array
'Pro' => int 15
'Info' => string 'likelihood of 15% '(length = 15)
'Num' => int 1596
1 =>
Array
'Pro' => int 25
'Info' => string 'likelihood of 25% '(length = 15)
'Num' => int 2484
2 =>
Array
'Pro' => int 40
'Info' => string 'likelihood of 40% '(length = 15)
'Num' => int 3939
3 =>
Array
'Pro' => int 20
'Info' => string 'likelihood of 20% '(length = 15)
'Num' => int 1981

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.