I need to distribute a number for multiple points in degraded order.
For example,
N = 100 // this 100 is fixed
no_of_points = 10 // no of points will vary
Need to break 100 into 10 points in descending order RANDOMLY.
e.g:
80, 55, 32, 18, 10, 10, 10, 8, 2, 0
Here,
I'm trying to do something like:
private static function generateRandomPercentage($no_of_points)
{
$distributions = [];
// need to start from 80
// but it may vary also
$max = mt_rand(80, 81);
$ratio = $max / $no_of_points;
for( $i = 1; $i <= $no_of_points; $i++ ) {
$delta = ($ratio * $i);
$distributions[] = round(($max - $delta) / 100, 2);
}
print_r($distributions);
}
But nothing working. Please help me.
Can be done with php standard functions:
$N = 100 // this 100 is fixed
$no_of_points = 10 // no of points will vary
$distributions= rsort(array_slice(shuffle(range(1,$N)), 0, $no_of_points);
print_r($distributions));
range = create array with values 1...n
shuffle = shuffle array
array_slice = return part of an array
rsort = sort array values hight to low values