简体   繁体   中英

Cant get Random to generate number between two numbers

Using this code I have been trying to get a number between them
I am using a double so I can use negative numbers

double minX = plugin.getConfig().getDouble("location.min.x");
double maxX = plugin.getConfig().getDouble("location.max.x");
double randomX = random.nextDouble(maxX-minX) + minX;

But I get a

The method nextDouble() in the type Random is not applicable for the arguments (double)

But if I set them to ints it works perfectly fine.

  • nextDouble() ,...Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence.

So change it to:

double randomX = random.nextDouble()*(maxX - minX) + minX;

The method: Random.nextDouble(), like Random.nextBoolean() and a few other methods of the Random class, does not take any upper bound arguments. A common workaround/solution is the one provided by flakes above, ie:

double randomX = random.nextDouble()*(maxX - minX) + minX;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM