PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
Copy Clear
Copy Format Clear
<?php $addends = []; $max_count = 9; $max_sum = 26; $max_value = 4; for($i=0; $i<=$max_count; $i++) { // generate random value between 0 and $max_sum, but not grate when $max_value $value = rand(1, min($max_value, $max_sum)); // echo "rand(0, min($max_value, $max_sum)) = $value \n"; if($value == $max_value){ continue; $i--; } else { // decrease $max_sum to $value $max_sum -= $value; // store value into array $addends[] = $value; } } print_r($addends); print_r(array_sum($addends));
Show:  
Copy Clear