3v4l.org

run code in 300+ PHP versions simultaneously
<?php function getRandWithStipulations(int $total, int $count, array $scope): array { if ($scope[0] > $scope[1]) { throw new Exception('Argument 3 (\$scope) is expected to contain a minimum integer then a maximum integer.'); } if ($scope[1] * $count < $total) { throw new Exception('Arguments 2 (\$count) and 3 (\$scope) cannot satisfy argument 1 (\$total).'); } $result = []; for ($x = 1; $x < $count; ++$x) { // count - 1 iterations $scope[0] = max($scope[0], $total - ($scope[1] * ($count - $x))); $scope[1] = min($scope[1], $total - ($scope[0] * ($count - $x))); $rand = rand(...$scope); $result[] = $rand; $total -= $rand; } $result[] = $total; return $result; } var_export(getRandWithStipulations(130, 3, [23, 70]));
Output for git.master
array ( 0 => 32, 1 => 55, 2 => 43, )
Output for git.master_jit
array ( 0 => 65, 1 => 25, 2 => 40, )

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
32.84 ms | 406 KiB | 5 Q