我的命运之轮旋转着,并且有机会赢得每项奖金,总计达到100%。一旦满足奖品的数量,即所有奖品都没有库存了,它会将概率设置为0,这样您就无法再次赢得它。那么我的问题是,其他值之和不等于100。它们总是必须等于100,否则轮子不起作用。
我已经到了将所有值除以总数,四舍五入到小数点后两位相加的程度,但是我得到的是99而不是100。我尝试了几种其他方法来实现永远无法达到100。
编辑:我的主要问题似乎是有0.01%的机会赢得2个奖项。将它们除以总数将得出0.000234234%或类似的值,因此当我将其乘以100并将其舍入到小数点后2位时,我得到0.00%-但我想不出其他办法...
$jsonString = file_get_contents('wp-content/themes/supertheme/wheel_data.json');
$data = json_decode($jsonString, true);
echo '<pre>';
//print_r($data['segmentValuesArray'][0]['probability']);
echo '</pre>';
if ($prize1 <= 5){
$prize1before = $data['segmentValuesArray'][0]['probability'];
$data['segmentValuesArray'][0]['probability'] = 0;
}
if ($prize2 >= 5){
$data['segmentValuesArray'][1]['probability'] = 0;
}
if ($prize3 >= 300){
$data['segmentValuesArray'][2]['probability'] = 0;
}
if ($prize4 >= 500){
$data['segmentValuesArray'][3]['probability'] = 0;
}
if ($prize5 >= 10){
$data['segmentValuesArray'][4]['probability'] = 0;
}
$prize1total = $data['segmentValuesArray'][0]['probability'];
$prize2total = $data['segmentValuesArray'][1]['probability'];
$prize3total = $data['segmentValuesArray'][2]['probability'];
$prize4total = $data['segmentValuesArray'][3]['probability'];
$prize5total = $data['segmentValuesArray'][4]['probability'];
$total = $prize1total + $prize2total + $prize3total + $prize4total + $prize5total;
echo'<pre>';
print_r($total);
echo'<pre>';
$a = 0;
foreach($data['segmentValuesArray'] as $prize_array){
if($prize_array['probability'] > 0){
$a++;
}
}
$integer = $prize1before / $a;
$divided1 = $prize1total / $total;
$rounded1 = number_format((float)$divided1, 2, '.', '');
$full1 = $rounded1 * 100;
$divided2 = $prize2total / $total;
$rounded2 = number_format((float)$divided2, 2, '.', '');
$full2 = $rounded2 * 100 + $integer;
$divided3 = $prize3total / $total;
$rounded3 = number_format((float)$divided3, 2, '.', '');
$full3 = $rounded3 * 100 + $integer;
$divided4 = $prize4total / $total;
$rounded4 = number_format((float)$divided4, 2, '.', '');
$full4 = $rounded4 * 100 + $integer;
$divided5 = $prize5total / $total;
$rounded5 = number_format((float)$divided5, 2, '.', '');
$full5 = $rounded5 * 100 + $integer;
$newtotal = $full1 + $full2 + $full3 + $full4 + $full5;
echo'<pre>';
print_r($newtotal);
echo'<pre>';
$newJsonString = json_encode($data, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
file_put_contents('wp-content/themes/supertheme/wheel_data.json', $newJsonString);