I want to receive a random result based on their chance.
For example if there are 5 cases:
A = 50%, B = 37,5%, C = 10%, D = 2,4%, E = 0,1%
What would the correct algorithm be to calculate their percentage?
I reviewed a few posts here on StackOverflow but I could not find one that uses floating numbers, that’s why I’m creating this post.
I tried using ThreadLocalRandom:
int random = ThreadLocalRandom.current().nextInt(100);
Based on the integer I’d return the given result. This works fine for whole number percentages, but fails with floating ones.
I experience the sam result using Math.random()
, obviously.
Is it correct for me to use this in this case or is there are better solution?:
ThreadLocalRandom.current().nextDouble(100.0);
Rengobli is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.