mardi 19 décembre 2017

Uniform Random with Less Sample

I am working on a game AI and using some randoms for making the decisions more stochastic. I am using cocos2d::random()

My experiment with the current random() function showed that if I pick 10K samples, I get the desired result. However, in the game, it is not very possible to pick such samples and this makes the random function behave not uniform.

So, my question is whether there is a way to improve uniformity of the random function with less samples.

Here is an example how I am using the random() function for shooting decision:

if( mShouldRedecideCanShootDecision)
{
    mShouldRedecideCanShootDecision = false;

    int chance = randomInt(0, 100);
    int upTo = std::roundf( mistake * 100.f );

    mCanShoot = true;
    if(chance < upTo)
    {
        mCanShoot = false;
    }
}

randomInt() function based on cocos2d::random():

// Max is inclusive
int randomInt(int min, int max)
{
    if(min > max)
    {
        std::swap(min, max);
    }

    return min + ( cocos2d::random() % (max - min + 1) );
}




Aucun commentaire:

Enregistrer un commentaire