Generate random numbers with fix probability

余生颓废 提交于 2019-11-30 10:20:37

The single probability check with linear probability can be easily done with:

function checkWithProbability($probability=0.1, $length=10000)
{
   $test = mt_rand(1, $length);
   return $test<=$probability*$length;
}

For example, this will produce:

for($i=0; $i<10; $i++)
{
   var_dump(checkWithProbability(1/3));
}

Something like:

bool(false)
bool(true)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(true)
bool(false)

And you can use that principle to get your edges check with desired probability:

function checkWithSet(array $set, $length=10000)
{
   $left = 0;
   foreach($set as $num=>$right)
   {
      $set[$num] = $left + $right*$length;
      $left = $set[$num];
   }
   $test = mt_rand(1, $length);
   $left = 1;
   foreach($set as $num=>$right)
   {
      if($test>=$left && $test<=$right)
      {
         return $num;
      }
      $left = $right;
   }
   return null;//debug, no event realized
}

The idea is to use geometry probability - i.e. split some line part into pieces with corresponding length and then check to which part our random number belongs.


                 0.75  0.9
                  |    |
                  V    V
*--------*--*-----*-*--*--* <-- (length)
^        ^  ^       ^     ^
|        |  |       |     |
0      0.4 0.5     0.8    1

Sample will be:

$set = [
  1 => 0.4,
  2 => 0.1,
  3 => 0.25,
  4 => 0.05,
  5 => 0.1,
  6 => 0.1
];
for($i=0; $i<10; $i++)
{
   var_dump(checkWithSet($set));
}

With result like:

int(1)
int(2)
int(2)
int(6)
int(3)
int(1)
int(1)
int(6)
int(1)
int(1)

You can increase $length - in theory, this will increase "quality" of randomize check, but that's not too easy thing - because mt_rand() uses pseudo-random generator, Mersenne Twister (and in ideal case that's not true linear probability)

A quite simple approach would be to have an array with the length 100, writing your "faces" numbers in it, shuffle it and get the first element.

So for your example in that array are 40x 1, 10x 2, 25x 3.

Little code example (not tested):

$probabilities = array(
    1 => 40,
    2 => 10,
    3 => 25,
    4 => 5,
    5 => 10,
    6 => 10
);

$random = array();
foreach($probabilities as $key => $value) {
    for($i = 0; $i < $value; $i++) {
        $random[] = $key;
    }
}

shuffle($random);
echo $random[0];

In your case you might generate random from 1 to 100 and then:

if random in 1:40 -> face 1
elseif random in 41:50 -> face 2
and so on. 

Of course, real code would be a little more complex to get real ranges and not hardcoded ifs

I can think of a very simple solution. This one does not alter the random number generator's generation pattern but interprets the outcomes so as to suit your problem above. I'd ask the random number generator to generate numbers between 0 and 9. And then do the following mapping where I assign ranges of the generated number to values of my intrest based on the probability I am intered in assigning to that value:

If result <= 3, face=1
else if result <=5, face =2
else is result <=25 face =3
//and so on
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!