Random boolean with weight or bias
Here's what I'm using. Very similar to FracturedRetina's answer.
Random random = new Random();
// 20% chance
boolean true20 = (random.nextInt(5) == 0) ? true : false;
// 25% chance
boolean true25 = (random.nextInt(4) == 0) ? true : false;
// 40% chance
boolean true40 = (random.nextInt(5) < 2) ? true : false;
I was wondering if there is a better or more natural way of doing this.
The approach you're using already is fine.
* As far as I know, there's not a standard Java method that will make this code any shorter.
* For non-cryptographic purposes.
1) Yes, i think your approach is valid and I don't see another easier way.
2) There is a library for handling random numbers of different statistical distributions:
http://introcs.cs.princeton.edu/java/22library/StdRandom.java.html