Separation probability

I have the following code in PHP that works great (returns more or less 10 results every time it starts):

function GetAboutTenRandomNumbers()
{
    $result = array();

    for ($i = 0; $i < 240; $i++)
    {
        if (Chance(10, 240) === true)
        {
            $result[] = $i;
        }
    }

    echo '<pre>';
    print_r($result);
    echo '</pre>';

    return $result;
}

      

And the Chance () function looks like this:

function Chance($chance, $universe = 100)
{
    $chance = abs(intval($chance));
    $universe = abs(intval($universe));

    if (mt_rand(1, $universe) <= $chance)
    {
        return true;
    }

    return false;
}

      

Now I want to randomly split these 10 (average) results into the following 4 segments:

  • the first has a probability of 10% * 10 = 1
  • the second having a probability of 20% * 10 = 2
  • third, with a probability of 30% * 10 = 3
  • the fourth with a probability of 40% * 10 = 4

As you can see, the sum of all segments (1 + 2 + 3 + 4) is 10, so I have coded the following function for that.

function GetAboutTenWeightedRandomNumbers()
{
    $result = array();

    // Chance * 10%
    for ($i = 0; $i < 60; $i++)
    {
        if (Chance(10 * 0.1, 240) === true)
        {
            $result[] = $i;
        }
    }

    // Chance * 20%
    for ($i = 60; $i < 120; $i++)
    {
        if (Chance(10 * 0.2, 240) === true)
        {
            $result[] = $i;
        }
    }

    // Chance * 30%
    for ($i = 120; $i < 180; $i++)
    {
        if (Chance(10 * 0.3, 240) === true)
        {
            $result[] = $i;
        }
    }

    // Chance * 40%
    for ($i = 180; $i < 240; $i++)
    {
        if (Chance(10 * 0.4, 240) === true)
        {
            $result[] = $i;
        }
    }

    echo '<pre>';
    print_r($result);
    echo '</pre>';

    return $result;
}

      

The problem is that I have run the GetAboutTenWeightedRandomNumbers function dozens of times and the result is much lower than the result returned by the GetAboutTenRandomNumbers function. I'm sure I'm making a fundamental math mistake, I suspect where, but I don't know how to solve it.

+1


a source to share


2 answers


Actually you!



In your second pass, you are giving it 60 values ​​per pass instead of 240, so you will get about a quarter of the expected values ​​in that pass. Run each at 240 and use module 60 to get the range of values ​​you are looking for in each loop.

+3


a source


If you expect to DoIt_02()

return with the same number of results as DoIt_01()

, then yes, you are making a fundamental math error. The weights of your sections summing up to 10 mean nothing, because the weighted odds don't apply to the entire set of 0..240. It will return similar results if you run every bounded probability at 0..240 instead of 0..59, 60..119, etc.



By the way, your function Chance()

doesn't work a bit in this case, to get the probabilities that you seem to be trying to use, it must be either mt_rand(1, $universe) <= $chance

or mt_rand(0, $universe - 1) < $chance

.

+2


a source







All Articles