# Solved Percentage Chances

Discussion in 'Plugin Development' started by YoloSanta, Jun 20, 2017.

Not open for further replies.
1. Offline

### YoloSanta

this is a question off the top of my head so I have no code to show, but how would I make an event that has chances of something happening for example onPlayerJoinEvent there has a chance for them to get a diamond how would I do this ?

EDIT: with permissions could be more helpful for example playerjoin.chance.25 > 25% chance to get a diamond on join

#1
2. Offline

### MelonCola

Well for a 25% chance, 100/25 = 4. Therefor you could use https://stackoverflow.com/questions/5887709/getting-random-numbers-in-java#5887736 or the random class to do:
Code:
```Random rand = new Random();
int value = rand.nextInt(4);
if(value == 0){
//Do stuff
}```
Doing rand.nextInt(4); gives you a random number between 0-3 meaning you would have a 25% chance of getting 0. However, you have to remember that it is never random, and there is always a pattern of the numbers rand returns, so I would do some research on it.

#2
3. Offline

### YoloSanta

@MelonCola So something like this could do the job?

Code:java
`import java.util.Random; Random rand = new Random(); int n = rand.nextInt(4) + 1;`

#3
4. Offline

### MelonCola

Yes, doing that would result in n equaling a not so random random number between 1-4.

#4
5. Offline

### MCMastery

You shouldn't do that, it's harder to use and read.

Just make a method like this:
Code:
```public static Boolean percentChance(double chance) {
return Math.random() <= chance;
}```
Then in your case, you could do something like this for 25%:
Code:
```if (percentChance(0.25)) {
// do something
}```

#5