Jeel Shah
Jeel Shah

Reputation: 3334

Probability in Java

I was curious to know, how do I implement probability in Java? For example, if the chances of a variable showing is 1/25, then how would I implement that? Or any other probability? Please point me in the general direction.

Upvotes: 46

Views: 90154

Answers (5)

Thomas
Thomas

Reputation: 88707

Generally you use a random number generator. Most of those return a number in the interval [0,1[ so you would then check whether that number is < 0.04 or not.

if( new Random().nextDouble() < 0.04 ) {  //you might want to cache the Random instance
   //we hit the 1/25 ( 4% ) case.
}

Or

if( Math.random() < 0.04 ) {
  //we hit the 1/25 ( 4% ) case.
}

Note that there are multiple random number generators that have different properties, but for simple applications the Random class should be sufficient.

Edit: I changed the condition from <= to < because the upper boundary of the random number is exlusive, i.e. the largest returned value will still be < 1.0. Hence x <= 0.04 would actually be slightly more than a 4% chance, while x < 0.04 would be accurate (or as accurate as floating point math can be).

Upvotes: 36

Wald
Wald

Reputation: 69

Since 1.7 it's better to use (in concurrent environment at least):

ThreadLocalRandom.current().nextInt(25) == 0

Javadoc

A random number generator isolated to the current thread. Like the global Random generator used by the Math class, a ThreadLocalRandom is initialized with an internally generated seed that may not otherwise be modified. When applicable, use of ThreadLocalRandom rather than shared Random objects in concurrent programs will typically encounter much less overhead and contention. Use of ThreadLocalRandom is particularly appropriate when multiple tasks (for example, each a ForkJoinTask) use random numbers in parallel in thread pools. Usages of this class should typically be of the form: ThreadLocalRandom.current().nextX(...) (where X is Int, Long, etc). When all usages are of this form, it is never possible to accidently share a ThreadLocalRandom across multiple threads.

This class also provides additional commonly used bounded random generation methods.

Upvotes: 6

Michael Berry
Michael Berry

Reputation: 72294

You'd use Random to generate a random number, then test it against a literal to match the probability you're trying to achieve.

So given:

boolean val = new Random().nextInt(25)==0;

val will have a 1/25 probability of being true (since nextInt() has an even probability of returning any number starting at 0 and up to, but not including, 25.)

You would of course have to import java.util.Random; as well.

As pointed out below, if you're getting more than one random number it'd be more efficient to reuse the Random object rather than recreating it all the time:

Random rand = new Random();
boolean val = rand.nextInt(25)==0;

..

boolean val2 = rand.nextInt(25)==0;

Upvotes: 76

narek.gevorgyan
narek.gevorgyan

Reputation: 4185

Maybe you can implement this with generating random numbers.

  Random rn = new Random();
    double d = rn.nextDouble();     // random value in range 0.0 - 1.0
    if(d<=0.04){
    doSomeThing();
    }

Upvotes: 3

Jack Edmonds
Jack Edmonds

Reputation: 33171

Java has a class called java.util.Random which can generate random numbers. If you want something to happen with probability 1/25, simply generate a random number between 1 and 25 (or 0 and 24 inclusive) and check whether that number is equal to 1.

if(new java.util.Random().nextInt(25)==0){
    //Do something.
}

Upvotes: 5

Related Questions