Home > Backend Development > PHP Tutorial > 分发红包的算法

分发红包的算法

WBOY
Release: 2016-06-13 12:16:34
Original
980 people have browsed it

求一个分发红包的算法
抢红包大家都玩过了,
就是给出一个总额,一个份数,就自动随机分配金额。

最小额0.01元。

求算法

function($总额,$红包个数){

。。。

return $红包数组;
}
------解决思路----------------------
改成这样可能好些

function distribute($total, $num) {<br />  $avg = $total / $num;<br />  $r = array_fill(0, $num, $avg);<br /><br />  for($i=0; $i<$num; $i+=2) {<br />    $t = rand(1, $avg * 100) / 100;<br />    $r[$i] -= $t;<br />    if($r[$i] <= 0) $r[$i] = 0.01;<br />    $r[$i+1] += $t;<br />  }<br />  $r[$num-1] = $total - array_sum(array_slice($r, 0, -1));<br />  return $r;<br />}<br /><br />
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template