基于这些获胜号码的5个数字的移动平均值是移动部分。因此,如果中奖号码为: 3-4-5-6-7-8-4-5-6-33-23-12-5-1 然后,第一个移动平均线基于3-4-5-6-7除以5 然后第二个移动平均线基于4-5-6-7-8除以5 然后,第三移动平均线基于5-6-7-8-4除以5
我尝试创建一个数组,但是没有用。
<?php
$existing = [];
$win_numbers = [];
for ($rnd=1;$rnd<=250;$rnd++)
{
$randoms[] = mt_rand(0,36); // see this block for generating randoms
}
echo "Random Numbers:<br>";
echo implode('-', $randoms).PHP_EOL;
echo "<br><br>";
$i = 0;
foreach($randoms as $rnd){
$i++;
if(in_array($rnd,$existing)){
$win_numbers[] = $i;
$i=1;
$existing = [];
}
$existing[] = $rnd;
}
echo "Win Numbers:<br>";
echo implode('-',$win_numbers);
echo "<br><br>";
echo "Moving Averages:<br>";
?>
答案 0 :(得分:1)
您可以这样做,也可以使用array_sum()
和array_slice()
之类的几种方法进行优化
<?php
// Function to print moving average
function movingAverage($win_numbers, $average_for)
{
foreach($win_numbers as $key=>$value){
$win_number_consecutive_5 = array_slice($win_numbers, $key, $average_for);
if(count($win_number_consecutive_5) == $average_for) {
$win_averages[] = array_sum($win_number_consecutive_5) / $average_for;
}
}
return $win_averages;
}
echo "Moving Averages:<br>";
$win_numbers = explode('-','3-4-5-6-7-8-4-5-6-33-23-12-5-1');
$average_for = 5;
echo implode('-',movingAverage($win_numbers, $average_for));
?>
工作演示: https://3v4l.org/GO0Ai
答案 1 :(得分:1)
如果使用for循环对数组进行循环并使用array_slice,则可以获得滚动平均值。
for($i=0;$i<count($randoms)-4;$i++){
$avg[] = array_sum(array_slice($randoms, $i, 5))/5;
}