How to make 5 random numbers with a total of 100

Do you know a way to divide an integer into ... 5 groups. Each total should be random, but the total should be a fixed number.

for example, I have "100". I want to break this number down

1- 20
2- 3
3- 34
4- 15
5- 18

EDIT: I forgot to say yes, the balance would be good. I suppose this can be done if the if statement blocks any number above 30 instances.

+5
source share
8 answers

Depending on how much you need it and how resource intensive is the environment in which you plan to run the script, you can try the following approach.

<?php
set_time_limit(10);

$number_of_groups   = 5;
$sum_to             = 100;

$groups             = array();
$group              = 0;

while(array_sum($groups) != $sum_to)
{
    $groups[$group] = mt_rand(0, $sum_to/mt_rand(1,5));

    if(++$group == $number_of_groups)
    {
        $group  = 0;
    }
}

An example of the generated result will look something like this. Pretty random.

[root@server ~]# php /var/www/dev/test.php
array(5) {
  [0]=>
  int(11)
  [1]=>
  int(2)
  [2]=>
  int(13)
  [3]=>
  int(9)
  [4]=>
  int(65)
}
[root@server ~]# php /var/www/dev/test.php
array(5) {
  [0]=>
  int(9)
  [1]=>
  int(29)
  [2]=>
  int(21)
  [3]=>
  int(27)
  [4]=>
  int(14)
}
[root@server ~]# php /var/www/dev/test.php
array(5) {
  [0]=>
  int(18)
  [1]=>
  int(26)
  [2]=>
  int(2)
  [3]=>
  int(5)
  [4]=>
  int(49)
}
[root@server ~]# php /var/www/dev/test.php
array(5) {
  [0]=>
  int(20)
  [1]=>
  int(25)
  [2]=>
  int(27)
  [3]=>
  int(26)
  [4]=>
  int(2)
}
[root@server ~]# php /var/www/dev/test.php
array(5) {
  [0]=>
  int(9)
  [1]=>
  int(18)
  [2]=>
  int(56)
  [3]=>
  int(12)
  [4]=>
  int(5)
}
[root@server ~]# php /var/www/dev/test.php
array(5) {
  [0]=>
  int(0)
  [1]=>
  int(50)
  [2]=>
  int(25)
  [3]=>
  int(17)
  [4]=>
  int(8)
}
[root@server ~]# php /var/www/dev/test.php
array(5) {
  [0]=>
  int(17)
  [1]=>
  int(43)
  [2]=>
  int(20)
  [3]=>
  int(3)
  [4]=>
  int(17)
}
+4

4 , 20 ( , , 40% 20, .. 8). , 100.

, . , , 4 ( ), , (total/n, 20), 100. - 5 , 5 , .

only 4 random point between 0 and 100

+5

. , , 10% .

n-1 (n - ), . - , , .

.

/**
 * Calculate n random numbers that sum y.
 * Function calculates a percentage based on the number
 * required, gives a random number around that number, then
 * deducts the rest from the total for the final number.
 * Final number cannot be truely random, as it a fixed total,
 * but it will appear random, as it based on other random
 * values.
 * 
 * @author Mike Griffiths
 * @return Array
 */
private function _random_numbers_sum($num_numbers=3, $total=500)
{
    $numbers = [];

    $loose_pcc = $total / $num_numbers;

    for($i = 1; $i < $num_numbers; $i++) {
        // Random number +/- 10%
        $ten_pcc = $loose_pcc * 0.1;
        $rand_num = mt_rand( ($loose_pcc - $ten_pcc), ($loose_pcc + $ten_pcc) );

        $numbers[] = $rand_num;
    }

    // $numbers now contains 1 less number than it should do, sum 
    // all the numbers and use the difference as final number.
    $numbers_total = array_sum($numbers);

    $numbers[] = $total - $numbers_total;

    return $numbers;
}

:

$random = $this->_random_numbers_sum();
echo 'Total: '. array_sum($random) ."\n";
print_r($random);

:

Total: 500
Array
(
    [0] => 167
    [1] => 164
    [2] => 169
)
+4
$number = 100;
$numbers = array();
$iteration = 0;
while($number > 0 && $iteration < 5) {
    $sub_number = rand(1,$number);
    if (in_array($sub_number, $numbers)) {
        continue;
    }
    $iteration++;
    $number -= $sub_number;
    $numbers[] = $sub_number;    
} 

if ($number != 0) {
    $numbers[] = $number;
}

print_r($numbers);
+3

, :

<?php
$tot = 100;
$groups = 5;
$numbers = array();
for($i = 1; $i < $groups; $i++) {
    $num = rand(1, $tot-($groups-$i));
    $tot -= $num;
    $numbers[] = $num;
}
$numbers[] = $tot;

, , .

+1

, , 100 - currentTotal

0

, , , , , 960. , .

// the range of the array 
$arry = range(1, 999, 1);
// howmany numbers do you want
$nrresult = 3;
do {
    //select three numbers from the array
    $arry_rand = array_rand ( $arry, $nrresult );
    $arry_fin = array_sum($arry_rand);
    // dont stop till they sum 960
} while ( $arry_fin != 960 );

//to see the results
foreach ($arry_rand as $aryid) {
    echo $arryid . '+ ';
}
0

, , , , .

, 100 , ,

foreach i from 1 to n
  group[ random(1,n) ] ++;

random(1, n/100) - , n.

However, you want to get a balance, so I believe that the best for you will be a normal distribution. Draw 5 Gaussian values ​​that will divide the number (their sum) into 5 parts. Now you need to scale these parts so that their sum is n and around them, so you get 5 groups.

0
source

All Articles