lundi 21 novembre 2016

Is there a more efficient way to create an x% chance of something happening?

Usually, when I want a chance of something happening I use a random number, then an if statement to check for a specific outcome, like so:

public static void main(String[] args){
    public boolean chance(){
        Random r = new Random();
        int chance = Random(100)+1; //For a 1/100 chance
        if(chance == 1){
            return true;
        }else{
            return false;
        }
    } 
}

Are there more efficient ways to do this in java, like a java library method, or am I stuck with creating it every time I want to use a percent chance.




Aucun commentaire:

Enregistrer un commentaire