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.
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.
The java. util. Random class provides more flexible ways to generate uniformly distributed random numbers, providing easy generation of types other than double, as well as providing a Gaussian distribution.
To simulate probability in Java, the first thing we need to do is to generate random numbers. Fortunately, Java provides us with plenty of random numbers generators. In this case, we'll use the SplittableRandom class because it provides high-quality randomness and is relatively fast:
The mathematic probability is a Number between 0 and 1. 0 indicates Impossibility and 1 indicates Certainty. The number of ways the event can happen / The number of possible outcomes.
Therefore, the probability of drawing 0 is equal to 10%. Now, let's get a random number and test if the chosen number is lower than the drawn one: Here, we drew numbers from 1 to 100. The chance for our random number to be lesser or equal to 50 is exactly 50%. 3. Uniform Distribution
The probability of an event which is certain to occur is one. The probability of an event which is impossible to zero. P (A)+ P ( A) = 1, 0 ≤ P (A) ≤ 1,0≤ P ( A )≤1. 1. Trial and Event: The performance of an experiment is called a trial, and the set of its outcomes is termed an event.
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;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With