Floating Point Random Methods

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   hear noise   go to next page

Answer:

Yes. They are necessary for simulations and in mathematical, scientific, and engineering programming.

Floating Point Random Methods

Here are some more methods of the Random class:

float nextFloat() — Returns a pseudorandom, floating point value in the range 0.0 up to but not including 1.0.
double nextDouble() — Returns a pseudorandom, double-precision value in the range 0.0 up to but not including 1.0.
double nextGaussian() — Returns a pseudorandom, double-precision value drawn from a Gaussian distribution (also called a "normal" distribution or a "bell-shaped curve".) The mean of the distribution is 0.0 and the standard deviation is 1.0.
floating point spinner

Notice that nextFloat() and nextDouble() output random floating point values in the range [0.0, 1.0), all values starting at zero, up to but not including 1.0. You might think that it is strange not to include 1.0, but this is acutally convenient, and is what is done by nearly all floating point random number generators.

To visualize a random floating point generator, think of a spinner centered on a circle with a circumference of one foot. To generate a random float, spin the pointer, then measure the distance along the circle, from the zero mark.

If the circumference is exactly one foot, then the "0.0" mark on the circle and the "1.0" mark on the circle would be the same. This might be awkward. But 1.0 is excluded from the range so the problem is avoided.

QUESTION 11:

Say that you want random doubles in the range 0.0 up to (but not including) 10.0. How can you get that range using nextDouble()?