The original posts have been affixed to the knot. Just get the result is not perfect, always the maximum number 999 repeats too much, seems not enough random, a little fake.
http://bbs.csdn.net/topics/391814341
$r = foo (32898), Echo array_sum ($r), Php_eol; Verify the Sum Print_r (array_count_values ($r)); View distribution function foo ($num, $k, $min =1, $max =999) { $res = Array_fill (0, $k, 1); Do {for ($i =0; $i < $k; $i + +) { $sum = array_sum ($res); $t = rand (0, $max-$min); if ($res [$i] + $t > $max) $t = $max-$res [$i]; if ($sum + $t > $num) $t = $num-$sum; $res [$i] + = $t; } } while ($num > $sum); return $res;}
Please fix this problem, thank you.
Reply to discussion (solution)
$t = rand (0, $max-$min);
Switch
$t = rand (0, $max-$res [$i]);
It's supposed to be a good distribution.
Array ( [886] = 1 [760] = 1 [470] = 1 [875] = 1 [591] = 1 [499] = 1 [25 7] = 1 [954] = 1 [~] + 1 [615] = 1 [770] = 1 [644] = 1 [971] = 1< C13/>[979] + 1 [259] = 1 [906] = 1 [830] = 1 [537] = 1 [595] = 1 [53 1] = 1 [588] = 1 [748] = 1 [767] = 1 [844] = 1 [931] = 1 [835] = 1 [986] + 1 [976] = 1 [[] + 1 [157] + 1 [408] = 1 [582] + 1 [ 593] + 1 [876] = 1 [710] = 1 [[] = 1 [554] = 1 [774] = 1 [386] =&G T 1 [339] = 1 [686] = 1 [194] = 1 [903] = 1 [304] = 1 [207] + 1 [ 978] + 1 [232] = 1 [618] = 1 [834] = 1 [472] = 1 [839] = 1 [377] =&G T 1 [788] = 1)