I am generating random numbers and I am having a equal distribution issue. The first and last numbers in the range always have half the change of getting chosen because they don't have that possibility of being rounded to on both sides.
For example, if the user chooses a min of 1 and a max of 10, 1 and 10 will have a decreased chance of getting picked compared to the others.
Here is my current code:
double num = random.nextDouble() * (max - min) + min;
String finalNum = String.format("%." + precision + "f", num);
I know I could fix this by using a nextInt() instead but the problem is that I want to keep it a double because the user selects how many decimal places there will be.
Help is appreciated.
num
right now forces it to be anint
. Switching tonextInt
won't change the precision at all. Any precision you're hoping for is already being discarded.