Jump to content

Featured Replies

Posted
comment_63566

Hey guys.

I see far too many servers using this method to generate percentage chance;

int chance = 10;
if (Misc.random(100) <= chance){
    doSomething;
}

I made this one to simplify things:

public static double randomDouble(double i) {
        return getRandomDouble(i);
    }

public static boolean percentageChance(double percentage) {
        return percentage >= randomDouble(100);
    }

You need the random double method in order for for the percentage chance to work. Shove them into Misc.java or something.

Use like this (This example is generating a 10% chance)

if (Misc.percentageChance(10.0)) {
            doSomething;
        }

 

Noice

  • 4 weeks later...
  • 3 weeks later...
  • 1 month later...
  • 3 months later...
  • 4 months later...
  • 3 months later...
  • 5 months later...

Create an account or sign in to comment