2
I have a value that I need to divide it into parts not equal with several numbers.
ex: if I receive the number 100, it could generate 20,30,10,20, 5, 15 = 100
the problem is that my code is wrong, the program hangs, does not leave the number 2.
function separate($val) {
if ($val == 1) {
return "1";
}
$list = [];
$total = 0;
while (true) {
$tmp = rand(1, $val);
if ($tmp + $total == $val) {
$list[] = $tmp;
$total += $tmp;
break;
} else {
$list[] = $tmp;
$total += $tmp;
}
}
return json_encode($list);
}
foreach (range(1, 500) as $m) { // gera ate 500 de prêmio
print "{$m}\n";
var_dump(separate($m));
print "=======\n";
}
could generate 20,30,10,20, 5,15, but is not in parts not equal? the 20 is repeated. And in how many parts would be?
– user60252
it doesn’t have to be exactly different, I think I expressed myself badly, because if it’s all the same I could divide by a number, but it has to have a little randomness, about the amount of 1-12 at most. and the numbers need to be integer.
– Bruno R