i need a algoritm that spreads some numbers across a day in percentages, following min / max percentage for that time rule, this is what i have atm:
public function getSpread() {
$rules = [
'00-07' => ['min' => 5, 'max' => 10],
'08-12' => ['min' => 20, 'max' => 30],
'13-16' => ['min' => 20, 'max' => 30],
'17-22' => ['min' => 25, 'max' => 40],
'23-24' => ['min' => 10, 'max' => 15],
];
$spread = [];
foreach ($rules as $time => $rule) {
$value = rand($rule['min'], $rule['max']);
while ($this->getNewSum($spread, $value) > 100) {
$value = $value - 1;
}
$spread[$time] = $value;
}
return $spread;
}
private function getNewSum($spread, $value) {
$sum = 0 + $value;
foreach ($spread as $s) {
$sum = $sum + $s;
}
return $sum;
}
Results:
array:5 [
"00-07" => 5
"08-12" => 24
"13-18" => 30
"19-22" => 32
"23-24" => 9
]
array:5 [
"00-07" => 8
"08-12" => 29
"13-18" => 29
"19-22" => 27
"23-24" => 7
]
array:5 [
"00-07" => 8
"08-12" => 28
"13-18" => 21
"19-22" => 33
"23-24" => 10
]
array:5 [
"00-07" => 9
"08-12" => 20
"13-18" => 21
"19-22" => 25
"23-24" => 15
]
array:5 [
"00-07" => 8
"08-12" => 28
"13-18" => 20
"19-22" => 29
"23-24" => 10
]
array:5 [
"00-07" => 9
"08-12" => 20
"13-18" => 22
"19-22" => 28
"23-24" => 15
]
array:5 [
"00-07" => 5
"08-12" => 23
"13-18" => 23
"19-22" => 40
"23-24" => 9
]
Any way to optimize this and make it more "random", id like to be able to increase the min, max rules, but if i do this, the end element suffers, and always becomes a 0
Id would actually be optimal to create this by the hour, rather then a from - to hour.