PHP generates random percentages

I was wondering if there is a quick way to generate the number of xrandom percentages that add up to 100%?

I tried to write this function, but more time than 100% was spent in the first 2 or 3 iterations, and the rest was only 0%, and I would like all percentages to exceed 0.

function randomPercentages($x) {

    $percent = 100;
    $return = array();

    for($i=1; $i <= $x; $i++) {
        $temp = mt_rand(1, $percent);
        $return[] = $temp;
        $percent -= $temp;
    }

    return $return;

}
print_r(randomPercentages(7));
+4
source share
3 answers

I edited your function to always reach 100%, at least 1% for each value (but the latter values ​​are often very low, and the trend increases with the number of percent to generate):

function randomPercentages($x) {
    $percent = 100;
    $return = array();
    for($i=1; $i <= $x; $i++) {
        if($i < $x) {
            $temp = mt_rand(1, ($percent-($x-$i)));
        } else {
            $temp = $percent;
        }
        $return[] = $temp;
        $percent -= $temp;
    }
    return $return;
}

And here is a rewritten and tested function with a great idea and code from Felk (but zero values ​​are possible):

function randomPercentagesFelk($x) {
    $temp[] = 0;
    for($i=1; $i<$x; $i++) {
        $temp[] = mt_rand(1, 99);
    }
    $temp[] = 100;
    sort($temp);
    $percentages = [];
    for($i=1; $i<count($temp); $i++) {
        $percentages[] = $temp[$i] - $temp[$i-1];
    }
    return $percentages;
}

" 0" ( 98 - , ). , : 98 0,01-0,04 :

function randomPercentagesFelkZero($x) {
    $temp[] = 0;
    for($i=1; $i<$x; $i++) {
        $new = mt_rand(1, 99);
        if($i<98) {
            while(in_array($new,$temp)) {
                $new = mt_rand(1, 99);
            }
        }
        $temp[] = $new;
    }
    $temp[] = 100;
    sort($temp);
    $percentages = [];
    for($i=1; $i<count($temp); $i++) {
        $percentages[] = $temp[$i] - $temp[$i-1];
    }
    return $percentages;
}
+3

1: 0 1. ( 0 100, )

2. .

EDIT: 2 1/2: 0 1 .

3: . - .

:

0.23, 0.65, 0.77

0.23 - 0.00 = 23%
0.65 - 0.23 = 42%
0.77 - 0.65 = 12%
1.00 - 0.77 = 23%
             ----
             100%

:

$num = 5;
$temp = [0.0];
for ($i=0; $i<$num; $i++) $temp[] = mt_rand() / mt_getrandmax();
sort($temp);
$temp[] = 1.0;

$percentages = [];
for ($i=1; $i<count($temp); $i++) $percentages[] = $temp[$i] - $temp[$i-1];
+5
<?php

//One way to code Felk solution :

function randomPercentages($x){
        $seeds = Array();
        for($i=0; $i < $x - 1; $i++){
                $seeds[] = rand(0,100);
        }
        sort($seeds);
        $results = [];
        for($i=0; $i < $x - 1; $i++){
                $last = $i == 0 ? 0 : $seeds[$i-1];
                $results[] = $seeds[$i] - $last;
        }
        $results[] = 100 - $seeds[$x-2];
        return $results;
}

print_r(randomPercentages(7));

outputs:

Array
(
    [0] => 20
    [1] => 0
    [2] => 12
    [3] => 11
    [4] => 4
    [5] => 28
    [6] => 25
)
+2
source

All Articles