samedi 5 septembre 2020

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. I am trying this way with for loop:

        $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. While I just want this to be generated in float value like 1.21, 0.10, 2.25 like so. I it possible to do that?




Aucun commentaire:

Enregistrer un commentaire