mercredi 18 mars 2020

PHP: How to select maximum probability value randomly?

I have below array & code

    $a = [
      149 => 55,
      130 => 10,
      131 => 5,
      132 => 5,
      133 => 10,
      134 => 10,
      135 => 5
    ];

   $rand = rand (0,(count($a)-1));

   echo array_values($a)[$rand];

This will give majorly result as 5,10 instead of 55.

Total of value is 100% probability. Values can be in decimal as well like 55.55, 10.10, etc. but overall going to be 100%

I already followed https://www.geeksforgeeks.org/how-to-get-random-value-out-of-an-array-in-php/

But this is not giving perfect result as expected.

So which has highest probability should be selected majorly and randomly.

So result can be like this : 55, 55, 10, 10, 10, 55, 5, etc..




Aucun commentaire:

Enregistrer un commentaire