mardi 1 mars 2016

Java: Applying probability constraints on Random()

I've never been particularly savvy with the Java random functions.

I'm using

int myrandom = ThreadLocalRandom.current().nextInt(x, y+1)

Where the value of myrandom will be calculated between the variables x and y.

The value it's self is not entirely relevant, and I will probably just set it to 0 and 1000, and then run an algorithm to interpret that value.

        for(int i = 0; i < 4; i++){

            if( randmods[i].gtr_less(choice) ){
                choice = i;
                break;

            }else{
                continue;

            }
        }

Where the function is simply:

public boolean gtr_less(int value){

    if(value > mod_min && value < mod_max){
        return true;

    }else{

        return false;
    }

}

The basic method is assessing all of the possibilities using if(rand > x && rand < y)

The problem is, I want to constrain the modifiers I place on each range within the 0-1000.

So if it was 10 cases and they were:

Z>0 && Z<100, Z>100 && Z<200, Z>200 && Z<300...

... and so on, I want to change the constraints to say, Z>0 && Z<110, Z>110 Z<210.

As you can see, and probably guess, manipulating the data in this way is a huge pain, and I am certain there must be a better way to do this, probably using modulus.

The main issue is that I have to modify /all/ of the cases when I only want to change one. Things get considerably more complicated when I have to restore defaults to just one, but not the others.

My math skills are not that sharp, so any help is appreciated.




Aucun commentaire:

Enregistrer un commentaire