我想进行自定义彩票提取,以激励用户参与在线实验.规则是:
> 10%即可获得10美元
> 1%几率获得50美元
> 0.1%几率获得500美元
彩票是一个PHP函数,它被调用一次并返回奖品(0,10,50或500).我创建了下面的函数,经过70 000次试验后,统计数据是:
10美元> 9.11%
> .91%,50美元
> .01%,500美元
我应该担心这个算法吗?有没有比mt_rand更好的方法来创造良好的机会分配?
function lottery() {
// winnings before extraction
$win=0;
// choose a winning number between 1 and 10
$target=mt_rand(1,10);
// make three independent extractions,each with 1/10 probability
if (mt_rand(1,10) == $target) {
// if first extraction is the winning number -> prize=10
// probability: 1/10
$win=10;
if (mt_rand(1,10) == $target) {
// if second extraction is ALSO the winning number -> prize=50
// probability: 1/10 * 1/10
$win=50;
if (mt_rand(1,10) == $target) {
// if third extraction is ALSO the winning number -> prize=500
// probability: 1/10 * 1/10 * 1/10
$win=500;
}
}
}
// return the prize
return $win;
}
谢谢你帮助新手!