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
| <?php
$prize_arr = [
['title' => '500金币', 'id' => 5, 'weight' => 1],
['title' => '免禁buff48小时体验', 'id' => 1, 'weight' => 2],
['title' => '防偷buff48小时体验', 'id' => 2, 'weight' => 2],
['title' => '免禁buff24小时体验', 'id' => 3, 'weight' => 3],
['title' => '防偷buff24小时体验', 'id' => 4, 'weight' => 3],
['title' => '100金币', 'id' => 6, 'weight' => 3],
['title' => '50金币', 'id' => 7, 'weight' => 6],
['title' => '5金币', 'id' => 8, 'weight' => 10],
];
function getRand($prize_arr) {
$result = count($prize_arr)-1; // 默认数组最后用一个
$pro_sum = array_sum(array_map('array_pop', $prize_arr)); // 拿到总数
// $pro_sum = array_sum(array_column($prize_arr, 'weight')); // 两个方法都ok
foreach ($prize_arr as $key => $value) {
$rand_num = mt_rand(1, $pro_sum);
if ($rand_num <= $value['weight']) {
$result = $key;
break;
} else {
$pro_sum -= $value['weight']; // 说明没有抽到,减去权重,继续来
}
}
return $result;
}
|