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 8.2.1
array ( 0 => 38, 1 => 68, 2 => 24, )
Output for 8.2.0
array ( 0 => 57, 1 => 36, 2 => 37, )
Output for 8.1.14
array ( 0 => 50, 1 => 26, 2 => 54, )
Output for 8.1.13
array ( 0 => 45, 1 => 38, 2 => 47, )
Output for 8.1.12
array ( 0 => 49, 1 => 50, 2 => 31, )
Output for 8.1.11
array ( 0 => 64, 1 => 30, 2 => 36, )
Output for 8.1.10
array ( 0 => 33, 1 => 43, 2 => 54, )
Output for 8.1.9
array ( 0 => 25, 1 => 51, 2 => 54, )
Output for 8.1.8
array ( 0 => 49, 1 => 31, 2 => 50, )
Output for 8.1.7
array ( 0 => 53, 1 => 48, 2 => 29, )
Output for 8.1.6
array ( 0 => 50, 1 => 28, 2 => 52, )
Output for 8.1.5
array ( 0 => 54, 1 => 49, 2 => 27, )
Output for 8.1.4
array ( 0 => 68, 1 => 34, 2 => 28, )
Output for 8.1.3
array ( 0 => 40, 1 => 42, 2 => 48, )
Output for 8.1.2
array ( 0 => 32, 1 => 47, 2 => 51, )
Output for 8.1.1
array ( 0 => 55, 1 => 23, 2 => 52, )
Output for 8.1.0
array ( 0 => 28, 1 => 48, 2 => 54, )
Output for 8.0.27
array ( 0 => 65, 1 => 28, 2 => 37, )
Output for 8.0.26
array ( 0 => 50, 1 => 55, 2 => 25, )
Output for 8.0.25
array ( 0 => 64, 1 => 25, 2 => 41, )
Output for 8.0.24
array ( 0 => 28, 1 => 36, 2 => 66, )
Output for 8.0.23
array ( 0 => 58, 1 => 49, 2 => 23, )
Output for 8.0.22
array ( 0 => 62, 1 => 23, 2 => 45, )
Output for 8.0.21
array ( 0 => 59, 1 => 34, 2 => 37, )
Output for 8.0.20
array ( 0 => 45, 1 => 55, 2 => 30, )
Output for 8.0.19
array ( 0 => 62, 1 => 41, 2 => 27, )
Output for 8.0.18
array ( 0 => 44, 1 => 45, 2 => 41, )
Output for 8.0.17
array ( 0 => 44, 1 => 32, 2 => 54, )
Output for 8.0.16
array ( 0 => 60, 1 => 29, 2 => 41, )
Output for 8.0.15
array ( 0 => 56, 1 => 25, 2 => 49, )
Output for 8.0.14
array ( 0 => 63, 1 => 34, 2 => 33, )
Output for 8.0.13
array ( 0 => 29, 1 => 60, 2 => 41, )
Output for 8.0.12
array ( 0 => 52, 1 => 54, 2 => 24, )
Output for 8.0.11
array ( 0 => 51, 1 => 47, 2 => 32, )
Output for 8.0.10
array ( 0 => 49, 1 => 56, 2 => 25, )
Output for 8.0.9
array ( 0 => 49, 1 => 25, 2 => 56, )
Output for 8.0.8
array ( 0 => 23, 1 => 64, 2 => 43, )
Output for 8.0.7
array ( 0 => 50, 1 => 39, 2 => 41, )
Output for 8.0.6
array ( 0 => 56, 1 => 35, 2 => 39, )
Output for 8.0.5
array ( 0 => 65, 1 => 42, 2 => 23, )
Output for 8.0.3
array ( 0 => 44, 1 => 47, 2 => 39, )
Output for 8.0.2
array ( 0 => 34, 1 => 60, 2 => 36, )
Output for 8.0.1
array ( 0 => 25, 1 => 60, 2 => 45, )

preferences:
50.62 ms | 464 KiB | 5 Q