float minX = 50.0f;
float maxX = 100.0f;
Random rand = new Random();
float finalX = rand.nextFloat(maxX - minX + 1.0f) + minX;
"The method nextFloat() in the type Random is not applicable for the arguments (float)"
Um, what?
The nextFloat() method of Random class returns the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from the random number generator's sequence.
In order to generate Random float type numbers in Java, we use the nextFloat() method of the java. util. Random class. This returns the next random float value between 0.0 (inclusive) and 1.0 (exclusive) from the random generator sequence.
nextFloat() method scans the next token of the input as a float. This method will throw InputMismatchException if the next token cannot be translated into a valid float value as described below. If the translation is successful, the scanner advances past the input that matched.
The nextFloat
method doesn't take an argument. Call it, then scale the returned value over the range you want.
float minX = 50.0f;
float maxX = 100.0f;
Random rand = new Random();
float finalX = rand.nextFloat() * (maxX - minX) + minX;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With