# Probability system

Discussion in 'Spigot Plugin Development' started by --TOMAS--, Apr 14, 2017.

1. ### --TOMAS--

Hi guys! I'm making a probability system. For example:
If a player has specific permission, it has a 5% chance of obtain 1 more diamond when mining, but if it has other permission has a 10% chance of obtain 2 more diamonds when mining.

I know the class Random, and Math.Random(), but with this I can't determine percentages.

2. ### uksspy

Yes you can. If Math.random() returns a number in the range (0, 1) then there is a 10% chance that the number will be in the range (0, 0.1), no? Using that principle you can easily make conditions that are based certain percent probability.

• Like x 2
3. ### DarkSeraphim

(or if you want to go easy on yourself, just use ThreadLocalRandom with ThreadLocalRandom::nextInt(int) - also a bit faster, because ThreadLocalRandom doesn't require synchronization)

• Like x 1
• Agree x 1
4. ### --TOMAS--

But, for 5% and other chances? I don't underestand why 0, 1 is 10% chance

5. ### uksspy

Think of 0 to 1 on a number line. The part from 0 to 0.1 (1/10) is 10% of the total area. Therefore, there is a 10% change the randomly generated number will land between those two numbers. If you can't extrapolate that process to other percents than there is not much help we can provide you.

6. ### DizMizzer

Use
Code (Text):
Random r = new Random();
int chance  =r.nextInt(100);
if (chance <= playerChance) {
//Run Code
}

• Agree x 1
7. ### NikoTheDev

Code (Text):
public static Boolean getRandomChance(double chance) {
Random rand = new Random();
int random = rand.nextInt(100);

if (random <= chance) {
return true;
}
return false;
}

• Agree x 1
• Creative x 1
8. ### FlyingLlama

Look up a "Weighted random". It is a simple algorithm for picking one option out of several with different chances.

9. ### FlyingLlama

What the heck lol.

• Agree x 1

What xD

11. ### FlyingLlama

That does absolutely nothing useful and is basically the same as one line of code (not to mention it makes an entirely new random for no clear reason, instead of using Math.random or much faster ThreadLocalRandom): if(r.nextDouble()<chance)

13. ### Mas

1. You should favour returning the primitive type 'boolean' over Boolean.
2. Use SplittableRandom or ThreadLocalRandom, they are both much faster alternatives than Random.
3. No need to create a new Random instance each time the method is run.
4. Spoonfeeding. People won't learn as much if you're just throwing code at them, especially if that code is inefficient or can be simplified: Math.random() <= chance

14. ### megamichiel

If you care about performance, use integers instead of floating points. RNGs work with integers so there's no pointless conversions and all that stuff.

15. ### DarkSeraphim

You should've used a RangeMap instead of a List, which gives you O(log n) lookups instead of O(n) lookups. As for duplicate checking, use Sets for O(1) contains checks.
Aside performance, it makes reasoning about results easier (since you're only dealing with natural numbers)

@DizMizzer @MrBandit don't instantiate new Random objects every call, but create a private static final field instead (of course, this doesn't beat ThreadLocalRandom in performance, it's still overall the fastest Random implementation out there)

16. ### Mas

I did some experiments a while back and found that SplittableRandom was marginally faster. I guess I'll try it again because I see lots of people saying TLR is faster

17. ### DarkSeraphim

I've done a benchmark with JMH here, which shows that TLR is faster.

• Like x 1