抽中概率 php
2016-01-26 20:51
615 查看
<?php /** *@FileName rand.php 抽中红包概率测试 **/ header("Content-type: text/html; charset=utf-8"); /** * 抽奖 * @param int $total 1为百分百中奖 2为二分之一中奖率 3为三分之一中奖率 以此类推... */ function getReward($total=10) { if($total == 1){ return 1; } $win1 = ($total-1)/100; $other = ($total-1)-$win1; $return = array(); for ($i=0;$i<$win1;$i++) { $return[] = 1;//中奖 } for ($n=0;$n<$other;$n++) { $return[] = 0;//不中奖 } //print_r($return); shuffle($return);//把数组中的元素按随机顺序重新排序 return $return[array_rand($return)]; } $data = getReward();//返回1为中奖 echo $data;die; ?>
/**
* 抽奖方法2
* @param int total概率(total/100)%
*/
function getReward($total=10) { if($total == 0){ return 0; } for ($i=0;$i<$total;$i++) { $return[] = 1;//中奖 } $other = 100-$total; for ($n=0;$n<$other;$n++) { $return[] = 0;//不中奖 } //print_r($return); shuffle($return);//把数组中的元素按随机顺序重新排序 return $return[array_rand($return)]; }
相关文章推荐
- 一个关于if else容易迷惑的问题
- PHP5.2.*防止Hash冲突拒绝服务攻击的Patch
- 深入理解PHP之匿名函数
- JSP/PHP基于Ajax的分页功能实现
- 关于PHP通过PDO用中文条件查询MySQL的问题。
- 什么是设计模式
- PHP数据库长连接mysql_pconnect的细节
- Php Installing An Expansion
- PHP+Apache在Windows 9x下的安装和配置
- IIS 6 的 PHP 最佳配置方法
- 安装Apache和PHP的一些补充
- Linux Apache+MySQL+PHP
- 建立Apache+PHP+MySQL数据库驱动的动态网站
- PHP 5.3.0 安装分析心得
- apache 环境下 php 的配置注意事项
- jQuery实现大转盘抽奖活动仿QQ音乐代码分享
- ASP.NET、ASP、PHP、JSP之间有什么区别?
- PHP VBS JS 函数 对照表
- C#实例代码之抽奖升级版可以经表格数据导入数据库,抽奖设置,补抽