Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random.nextFloat is not applicable for floats?

Tags:

java

random

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?

like image 651
herpderp Avatar asked May 20 '11 22:05

herpderp


People also ask

What is randomizer nextFloat?

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.

How do you generate a random float in Java?

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.

What is the purpose of nextFloat?

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.


1 Answers

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;
like image 191
Bill the Lizard Avatar answered Oct 14 '22 07:10

Bill the Lizard