We know that the classic range random function is like this:
public static final int random(final int min, final int max) {
Random rand = new Random();
return min + rand.nextInt(max - min + 1); // +1 for including the max
}
I want to create algorithm function for generating number randomly at range between 1..10, but with uneven possibilities like:
1) 1,2,3 -> 3/6 (1/2)
2) 4,5,6,7 -> 1/6
3) 8,9,10 -> 2/6 (1/3)
Above means the function has 1/2 chance to return number between 1 and 3, 1/6 chance to return number between 4 and 7, and 1/3 chance to return number between 8 and 10.
Anyone know the algorithm?
UPDATE:
Actually the range between 1..10 is just served as an example. The function that I want to create would apply for any range of numbers, such as: 1..10000, but the rule is still same: 3/6 for top range (30% portion), 1/6 for middle range (next 40% portion), and 2/6 for bottom range (last 30% portion).
Use the algorithm:
This should do the trick.
EDIT: As requested in your comment: