游戏转盘PHP后台实现奖品发放
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| <?php
header("Content-type:text/html;charset=GB2312");
$prizeArr = array( 0 => array('id' => 1, 'prize' => '现金500W', 'v' => 1), 1 => array('id' => 2, 'prize' => 'iphone7', 'v' => 5), 2 => array('id' => 3, 'prize' => '耐克跑鞋', 'v' => 10), 3 => array('id' => 4, 'prize' => '魔声耳机', 'v' => 24), 4 => array('id' => 5, 'prize' => '蓝牙音响', 'v' => 60), 5 => array('id' => 6, 'prize' => '现金1元', 'v' => 100) );
$item = []; foreach ($prizeArr as $k => $v) { $item[$v['id']] = $v['v']; }
function getRand($item) { $num = array_sum($item); $result = 0; foreach ($item as $k => $v) {
$rand = mt_rand(1, $num);
if ($rand <= $v) { $result = $k;
break; } else { $num -= $v;
} }
return $result; }
$times = 5000000; $prizes = []; for ($i = 0; $i < $times; $i++) { $r = getRand($item); $p = $prizeArr[$r - 1]['prize']; if (isset($prizes[$p])) { $num = $prizes[$p]; $num++; $prizes[$p] = $num; } else { $prizes[$p] = 1; } }
var_dump('概率:'); var_dump($prizes); var_dump('<br><br>'); foreach ($prizes as $p => $nums) { var_dump($p . '------>' . $nums / $times); var_dump('<br><br>'); } var_dump('<br><br>'); var_dump('<br><br>'); foreach ($item as $p => $nums) { var_dump($prizeArr[$p - 1]['prize'] . '------>' . $nums / 200); var_dump('<br><br>'); }
|
Reference
写在最后
欢迎大家关注鄙人的公众号【麦田里的守望者zhg】,让我们一起成长,谢谢。