Ask the red packet algorithm again. there was a bug in the past, @ JXYCYFLM ,@

Source: Internet
Author: User
Ask the red envelope algorithm, the previous bug, @ JXYCYFLM, @ http://bbs.csdn.net/topics/391001579
The previous post is here.


Both algorithms are problematic.

The revised version provided by the moderator is incorrect when the number of red packets is singular. The sum is not equal to the total number.


Ask the two heroes for help.


Reply to discussion (solution)

$r = distribute(100, 31);echo array_sum($r), PHP_EOL;print_r($r);function distribute($total, $num) {  $avg = $total / $num;  $r = array_fill(0, $num, round($avg, 2));   for($i=0; $i<$num; $i+=2) {    $t = rand(1, $avg * 100) / 100;    $r[$i] -= round($t, 2);    if($r[$i] <= 0) $r[$i] = 0.01;    if(isset($r[$i+1])) $r[$i+1] += round($t, 2);  }  $r[$num-1] = $total - array_sum(array_slice($r, 0, -1));  return $r;}

$r = distribute(100, 31);echo array_sum($r), PHP_EOL;print_r($r);function distribute($total, $num) {  $avg = round($total / $num, 2);  $r = array_fill(0, $num, $avg);   for($i=0; $i<$num; $i+=2) {    $t = rand(1, $avg * 100) / 100;    $r[$i] -= $t;    if($r[$i] <= 0) $r[$i] = 0.01;    if(isset($r[$i+1])) $r[$i+1] += $t;  }  $r[$num-1] = $total - array_sum(array_slice($r, 0, -1));  return $r;}

100Array(    [0] => 1.03    [1] => 5.43    [2] => 0.09    [3] => 6.37    [4] => 0.65    [5] => 5.81    [6] => 2.24    [7] => 4.22    [8] => 1.66    [9] => 4.8    [10] => 2.53    [11] => 3.93    [12] => 0.14    [13] => 6.32    [14] => 2.85    [15] => 3.61    [16] => 0.72    [17] => 5.74    [18] => 0.34    [19] => 6.12    [20] => 1.31    [21] => 5.15    [22] => 1.1    [23] => 5.36    [24] => 0.31    [25] => 6.15    [26] => 1.68    [27] => 4.78    [28] => 0.66    [29] => 5.8    [30] => 3.1)

function distribute($total,$num){return _distribute($total,$num,$total/$num/2,0);}function _ceil($i){return ((int)($i*100))/100;}function _distribute($total,$num,$pj,$last_c){if($num==0){return array();}else if($num==1){return array($total);}else {$cz=_ceil(mt_rand(-$pj*100,$pj*100)/100);$n=_ceil($pj+$cz+$last_c);$res[]=$n;if($num-1>=0)$res=array_merge($res,_distribute($total-$n,$num-1,$pj,$pj-$cz));return $res;}}$ary=distribute(50,12);print_r(array_sum($ary));print_r($ary);

Provide entertainment red envelope code

function fn($total, $part, $min = 0.01) {$res = array_fill(0, $part, $min);$total -= $part * $min;for($i=0; $i<$part; $i++) $total -= ($res[$i] += round($i == $part - 1 ? $total : (rand(0, $total * 100)) / 100, 2)) - $min;return $res;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.