jeudi 30 juin 2016

Weird RNG behavior

I'm trying to make a black box method to test if my project is working correctly, but I'm running into some problems with the random number generation.

I have got the method laid out like this:

public static MathObject generateBlackBoxObject() { 
        double functionChance = 0.5;
        double maxValue = 10;
        double minValue = -10;

        if (rng.nextDouble() < functionChance)
        {
            double random = rng.nextDouble();

            if (random < 1/3)
            {
                return Add.apply(Utility.generateBlackBoxObject(), Utility.generateBlackBoxObject());
            }
            else if (random < 2/3)
            {
                return Multiply.apply(Utility.generateBlackBoxObject(), Utility.generateBlackBoxObject());
            }
            else
            {
                return Exponentiate.apply(Utility.generateBlackBoxObject(), Utility.generateBlackBoxObject());
            }
        }
        else
        {
            double random = rng.nextDouble();

            if (random < 1/2)
            {
                return Constant.create(rng.nextDouble() * (maxValue - minValue) + minValue);
            }
            else
            {
                return Variable.create(rng.nextDouble() * (maxValue - minValue) + minValue);
            }
        }
    }

It makes use of a single java.util.Random, but the problem I'm having is that it is most likely to select the Variable.create ... path, and then the Exponentiation.apply ... path. It is kind of unlikely to go for Multiply and never (!) calls Add.apply or Constant.create

It kind of seems like the rng likes to pick the higher values more often than lower or somehow the previous random double influences the next to be higher.

Can anyone tell from this what is happening and (most likely) what I'm doing wrong here?




Aucun commentaire:

Enregistrer un commentaire