我需要一个算法,按照该时间规则的最小/最大百分比将一些数字按百分比分布在一天中,这就是我拥有的 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;
}
结果:
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
]
任何优化它并使其更“随机”的方法,我都希望能够增加最小、最大规则,但如果我这样做,最终元素会受到影响,并且总是变成 0
Id 实际上是按小时创建它的最佳选择,而不是按小时创建。