您的位置:首页 > 编程语言 > PHP开发

抽中概率 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)];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php 抽奖 概率