Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Generating a random double within a range (inclusive of the min and max to the range)

Tags:

java

I need to generate a random double between -0.10 and 0.25 inclusive of -0.10 and 0.25. I don't completely understand random number generators with java yet so I'm not quite sure how to do this. I know that the code below generates a number within that range, but I'm almost 100% certain it's not inclusive yet. How would I change it to be inclusive of the min and max of the range?

public double randDouble() {
    //formula: (Math.random() * (max - min) + min
    //in this case, min = -0.10 and max = 0.25
    return (Math.random() * 0.35) - 0.10;
}

My question is different than the one @javaguy is talking about because no where on that thread does someone say how to make this inclusive on both ends. And I've tested this code, but haven't seen an output of -.10 or 0.25 so unless my tests just weren't big enough, I can't see how it's inclusive of both ends.

like image 528
Sirius Avatar asked Apr 11 '17 16:04

Sirius


People also ask

How do you generate a random double in a range in Java?

In order to generate Random double type numbers in Java, we use the nextDouble() method of the java. util. Random class. This returns the next random double value between 0.0 (inclusive) and 1.0 (exclusive) from the random generator sequence.

How can I generate a random number within a range but exclude some?

After generating the random number you've to put the "holes" back in the range. This can be achieved by incrementing the generated number as long as there are excluded numbers lower than or equal to the generated one. The lower exclude numbers are "holes" in the range before the generated number.

How do you generate a random number in Java inclusive?

Random rand = new Random(); int x = rand. nextInt(10); x will be between 0-9 inclusive. So, given the following array of 25 items, the code to generate a random number between 0 (the base of the array) and array.


2 Answers

Since you want values between -0.10 and 0.25 both inclusive, i would suggest a different approach to control the granularity of your random values. Using Math.random() will return values between 0.0(inclusive) and 1.0(exclusive), so using your approach you will never get the value of 0.25. Instead use the Random() class and use integers to get both ends inclusive.

Random randomValue = new Random();
int randomInt = randomValue.nextInt(3501) - 1000; //Will get integer values between -1000 and 2500
Double rangedValue = Double(randomInt)/10000.0 // Get values between -0.10 and 0.25 

Alternatively you can more decimal places by increasing the magnitude of the values passed into randomValue.nextInt() and accordingly altering the value being devided by in the last line.

like image 117
Sasang Avatar answered Sep 17 '22 19:09

Sasang


Your plan to obtain random double values inclusive of the limits is ill-conceived, since there is no guarantee that you will ever receive a value which is equal to either limit.

That's due to the huge precision of double, which means that the possibility of obtaining any exact given random double is astronomically slim.

You can have that random number issued billions of times, and you may get thousands of values that are very close, extremely close to the limit, and yet none of them may ever happen to be equal to the limit.

Therefore, you cannot have any logic that depends on a random double number being issued which will be equal to the limit, because that random number may never be yielded.

So, the solution to your problem is very simple: stop worrying about inclusive vs. exclusive, because all you can ever hope for is exclusive. That should make things more simple for you.

like image 34
Mike Nakis Avatar answered Sep 17 '22 19:09

Mike Nakis