简体   繁体   English

100 天 php 针对特定金额的随机浮动

[英]Random Float against an specific amount with 100 days php

I am trying to generate random values against '100' amount of value for 100 days.我正在尝试针对 100 天的“100”数量生成随机值。 I am trying this way with for loop:我正在用 for 循环尝试这种方式:

        $total_days = 100;
        $total_amount = 100;
        $arr = array();

        for($i = 0; $i < $total_days; ++$i)
        {
            $arr[] = rand(0.0, 1000.0);
        }

        $actual_sum = array_sum($arr);

        for($i = 0; $i < $total_days; ++$i)
        {
            $arr[$i] *= $total_amount /$actual_sum;
            //echo $arr[$i]."<BR><BR>";
            
            $value = $arr[$i];

            $genpackageroi = array(
                'userid' => $userid,
                'pkgid' => $pkgid,
                'pkgcount' => $pkcount,
                'amount' => $value,
                'created_at' => $created_at,
                'updated_at' => $updated_at,
            );

            DB::table('genpackageroi')->insert($genpackageroi);

        }

The random generated value is like 0.1245444 OR 1.1245545, lots of numbers after decimal.随机生成的值就像 0.1245444 OR 1.1245545,十进制后的很多数字。 While I just want this to be generated in float value like 1.21, 0.10, 2.25 like so.虽然我只是希望它以浮点值生成,例如 1.21、0.10、2.25,像这样。 I it possible to do that?我有可能这样做吗?

But I want to generate the random numbers in round numbers.但我想以整数生成随机数。 Because It dose not matches the actual amount after generating all the random numbers if we sum the generated random numbers.因为如果我们将生成的随机数相加,它与生成所有随机数后的实际数量不匹配。 Like if I generates the random numbers for amount of 100, then the sum of all generated random numbers should 100. This is what actually I want.就像如果我生成数量为 100 的随机数,那么所有生成的随机数的总和应该是 100。这就是我真正想要的。

What I gathered from your statement is that you want 100 floating numbers with 2 decimal places, and the sum of all these numbers should add up to 100.我从您的陈述中收集到的是,您需要 100 个带有 2 个小数位的浮点数,并且所有这些数字的总和应为 100。

This certainly is not the cleanest solution, but here goes:这当然不是最干净的解决方案,但这里有:

$total_days = 100;
$total_amount = 100;
$arr = array();

for($i = 0; $i < $total_days; ++$i)
{
    $arr[] = rand(0.0, 1000.0);
}
$actual_sum = array_sum($arr);
for($i = 0; $i < $total_days; ++$i)
{
        $y = $arr[$i] * ($total_amount /$actual_sum);
        $arr[$i] = round($y, 2); //round the numbers to 2 dp. Sum of all numbers could be greater than 100.
}
//hack, logic explained below
$maxElementKeys = array_keys($arr, max($arr)); //get max element's key
unset($arr[$maxElementKeys[0]]); //remove the max element from array, now the array contains 99 elements
$arr = array_values($arr); //rebase the keys

for($i = 0; $i < $total_days; ++$i) 
{  
    if($i < ($total_days - 1) )
        $value = $arr[$i]; 
    else 
        $value = $total_amount - array_sum($arr); //to get the 100th number, subtract from 100 to ensure the total always adds up to 100
    $genpackageroi = array(
        'userid' => $userid,
        'pkgid' => $pkgid,
        'pkgcount' => $pkcount,
        'amount' => $value,
        'created_at' => $created_at,
        'updated_at' => $updated_at,
    );
    DB::table('genpackageroi')->insert($genpackageroi);
}

The logic here is that round all numbers to 2dps.这里的逻辑是将所有数字四舍五入到 2dps。 The total of which would in most cases exceed 100 by a few dps.在大多数情况下,总数会超过 100 几个 dps。 Next, get the max random number in the array and remove it from the array to make it an array containing 99 elements.接下来,获取数组中的最大随机数并将其从数组中移除,使其成为包含 99 个元素的数组。 The max number is removed to ensure that the sum of the new array stays below well clear of 100. So, to get the 100th element, get the sum of the new array and subtract that value from 100. Now, the total of all elements of the array and the 100th element that was just calculated should add up to exactly 100, with negligible chance for failure.删除最大数以确保新数组的总和保持在 100 以下。因此,要获得第 100 个元素,请获取新数组的总和并从 100 中减去该值。现在,所有元素的总和数组和刚刚计算的第 100 个元素的总和应该正好是 100,失败的可能性可以忽略不计。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM