lundi 28 juin 2021

propability in php using random and range

I want to have a probability for a range of numbers to show less and others to show more. I had a JS code that works fine but I want to convert my code to php. I want to move to server-side. The idea of the code is that I will have a random number between 0 and 10000. I want for example the number from 0 t0 4 to have 0.05 probability.

This is my Old JS Code:

     function getRandomInt(max) {
            return Math.floor(Math.random() * max);
        }
        function range(start, end) {
            return Array(end - start + 1).fill().map((_, idx) => start + idx)
        }
        
        var randomVal = getRandomInt(10000);
            console.log(randomVal);

            var p1 = range(0, 4);//0.0005
            var p2 = range(5, 9);//0.0005
            var p3 = range(10, 19);//0.001
            var p4 = range(20, 519);//0.05
            var p5 = range(520, 1019);//0.05
            var p6 = range(1020, 1519);//0.05
            var p7 = range(1520, 2019);//0.05
            var p8 = range(2020, 3319);//0.13
            var p9 = range(3320, 4919);//0.16
            var p10 = range(4920, 6519);//0.16
            var p11 = range(6520, 8199);//0.168
            var p12 = range(8200, 9999);//0.18


            var stopAt;


            if (p1.includes(randomVal)) {
                console.log("in p1");
                stopAt = 20;
                
            }
            if (p2.includes(randomVal)) {
                console.log("in p2");
                stopAt = 50;
            }
            if (p3.includes(randomVal)) {
                console.log("in p3");
                stopAt = 70;
            }
            if (p4.includes(randomVal)) {
                console.log("in p4");
                stopAt = 80;
            }
            if (p5.includes(randomVal)) {
                console.log("in p5");
                stopAt = 135;
            }
            if (p6.includes(randomVal)) {
                console.log("in p6");
                stopAt = 165;
            }
            if (p7.includes(randomVal)) {
                console.log("in p7");
                stopAt = 190;
            }
            if (p8.includes(randomVal)) {
                console.log("in p8");
                stopAt = 230;

            }
            if (p9.includes(randomVal)) {
                console.log("in p9");
                stopAt = 265;
            }
            if (p10.includes(randomVal)) {
                console.log("in p10");
                stopAt = 292;
            }
            if (p11.includes(randomVal)) {
                console.log("in p11");
                stopAt = 320;
            }
            if (p11.includes(randomVal)) {
                console.log("in p12");
                stopAt = 350;
            }

I tried doing it this way in PHP. Its works but it gives random with the probability which I want

PHP:

$randomVal =rand(0,10000);
           

            floor($randomVal);
            
            $stopAt;


            if (in_array($randomVal,range(0, 4))) {
                //echo "p1";
                $stopAt = 20;
                
            }
            if (in_array($randomVal,range(5,9))) {
                //echo "p2";
                $stopAt = 50;
            }
            if (in_array($randomVal,range(10,19))) {
                //echo "p3";
                $stopAt = 70;
            }
            if (in_array($randomVal,range(20,519))) {
                //echo "p4";
                $stopAt = 80;
            }
            if (in_array($randomVal,range(520,1019))) {
                //echo "p5";
                $stopAt = 135;
            }
            if (in_array($randomVal,range(1020,1519))) {
                //echo "p6";
                $stopAt = 165;
            }
            if (in_array($randomVal,range(1520,2019))) {
                //echo "p7";
                $stopAt = 190;
            }
            if (in_array($randomVal,range(2020,3319))) {
                //echo "p8";
                $stopAt = 230;

            }
            if (in_array($randomVal,range(3320,4919))) {
                //echo "p9";
                $stopAt = 265;
            }
            if (in_array($randomVal,range(4920,6519))) {
                //echo "p10";
                $stopAt = 292;
            }
            if (in_array($randomVal,range(6520, 8199))) {
                //echo "p11";
                $stopAt = 320;
            }
            if (in_array($randomVal,range(8200, 9999))) {
                //echo 'p12';
                $stopAt = 350;
            }

I Also Searched and found a code but my PHP knowledge isn't that great so I didn't know how I can do it with my code

This is the code:

$weightedValues=array( 20 => 0.05, 50 => 0.05, 70 => 0.10, 80 => 5, 135 => 5, 165 => 5, 190=>5, 230 =>13, 265 =>16, 292 => 16, 320 => 16.8, 350 => 18);
function getRandomWeightedElement(array $weightedValues) {
    $rand = mt_rand(1, (int) array_sum($weightedValues));

    foreach ($weightedValues as $key => $value) {
      $rand -= $value;
      if ($rand <= 0) {
        return $key;
      }
        else{
            echo $rand;
        }
    }
  }



Aucun commentaire:

Enregistrer un commentaire