初期就这些需求,然后根据网上的资料,采用了一种阶段式抽取的方法,大家下面看一下整体的程序: 该程序是在ThinkPHP框架下完成的,使用了一些框架自带的类库和函数,下面我会逐一进行说明,控制器部分
抽奖算法需要满足的需求如下:
1.可以控制中奖的概率
2.具有随机性
3.最好可以控制奖品的数量
4.根据用户ID或者ip、手机号、QQ号等条件限制抽奖次数
初期就这些需求,然后根据网上的资料,采用了一种阶段式抽取的方法,大家下面看一下整体的程序:
该程序是在ThinkPHP框架下完成的,使用了一些框架自带的类库和函数,下面我会逐一进行说明,控制器部分:
代码如下 | |
/** * * * @lanfengye */ class ChoujiangAction extends Action { //抽奖的开始时间 var $begin_time="2012-12-25 14:00:00"; //开始时间 0-不限制 //抽奖的结束时间 var $stop_time="0"; //结束时间 0-不限制 //本次抽奖的奖项信息,必须按照从大到小的顺序进行填写,id为奖次,prize为中奖信息,v为中奖概率,num为奖品数量 //需要注意的是,该处也必须包含不中奖的信息,概率从小到大进行排序 var $prize_arr = array( '0' => array('id' => 1, 'prize' => '44元购买1G/年空间', 'v' => 1,'num'=>1), '1' => array('id' => 2, 'prize' => '55元购买1G/年空间', 'v' => 2,'num'=>2), '2' => array('id' => 3, 'prize' => '66元购买1G/年空间', 'v' => 5,'num'=>2), '3' => array('id' => 4, 'prize' => '77元购买1G/年空间', 'v' => 10,'num'=>3), '4' => array('id' => 5, 'prize' => '88元购买1G/年空间', 'v' => 15,'num'=>4), '5' => array('id' => 6, 'prize' => '99元购买1G/年空间', 'v' => 67,'num'=>10), ); //首页显示方法 public function index(){ //连接数据库,去获取本次中奖的人员名单 $Choujiang=M('Choujiang'); $this->assign('list', $Choujiang->where("rid>0")->order('id desc')->select()); unset($Choujiang); //在首页中显示抽奖的开始时间 $this->assign('begin_time',$this->begin_time); $this->display(); } /** * 生成中奖信息,ajax进行请求该方法,需要客户填写QQ号码 */ 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, '请正确填写QQ号码!'); exit; } if(!empty($this->begin_time) && time() $this->ajaxReturn(1, '抽奖还没有开始,开始时间为:'.$this->begin_time); exit; } if(!empty($this->stop_time) && time()>strtotime($this->stop_time)){ $this->ajaxReturn(1, '本次抽奖已经结束,结束时间为:'.$this->stop_time); exit; } //获取奖项信息数组,来源于私有成员 $prize_arr= $this->prize_arr; foreach ($prize_arr as $key => $val) { $arr[$val['id']] = $val['v']; } //$rid中奖的序列号码 $rid = $this->get_rand($arr); //根据概率获取奖项id $str = $prize_arr[$rid - 1]['prize']; //中奖项 $Choujiang=M('Choujiang'); //从数据库中获取特定QQ号已经参加抽奖的次数,如果大于等于3则提示次数用完 if($Choujiang->where("qq_no='{$qq_no}'")->count()>=3){ $str='您3次抽奖机会已经用完!'; $rid=0; //从数据库中获取特定奖项序号的次数,大于等于设置的最大次数则提示奖品被抽完,如果需要一直中最后一个纪念奖,则修改该处即可 }elseif ($Choujiang->where("rid={$rid}")->count()>=$prize_arr[$rid-1]['num']) { $str='很抱歉,您所抽中的奖项已经中完!'; $rid=0; } //生成一个用户抽奖的数据,用来记录到数据库 $data=array( 'rid'=>$rid, 'pop'=>$str, 'qq_no'=>$qq_no, 'input_time'=>time() ); //将用户抽奖信息数组写入数据库 $Choujiang->add($data); unset($Choujiang); //ajax返回信息 $this->ajaxReturn(1, $str); } /** * 根据概率获取中奖号码 */ private function get_rand($proArr) { $result = ''; //概率数组的总概率精度 $proSum = array_sum($proArr); //概率数组循环 foreach ($proArr as $key => $proCur) { $randNum = mt_rand(1, $proSum); if ($randNum $result = $key; break; } else { $proSum -= $proCur; } } unset($proArr); return $result; } } ?> |
该算法简单使用,并发访问性能非常好,稍加改动就可以用于各种场合,结合用户登录等信息可有效控制每个人的抽奖次数。将开始和结束之间更改为数组,就可以完善成为每天特定时间抽奖的程序。

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),