Skip to content
Advertisement

Uniform random distribution in java

i’m generating Random values with range of 5.0 – 9.0, are the numbers generated still uniformly distributed theoretically ??

and how to implement the histogram, i mean what values we need to implement the histogram ??

import java.util.Random;

public class GenerateNumber {
    public static void main(String args[]) {

        Random randomGenerator = new Random();

        for (int i = 0; i < 11; i++) {
            double randomDou = randomGenerator.nextDouble() * (9 - 5 + 1) + 5;
            System.out.println(randomDou);
        }
    }
}
  • i’m zero on math .., pls help me

Advertisement

Answer

You are on the righ path- scaling the numbers linearly is correct.

To test your logic, plug in 0.0 and 1.0 as values from nextDouble()

0.0 * (9 - 5 + 1) + 5 = 5, o.k.
1.0 * (9 - 5 + 1) + 5 = 10, not o.k.

This is a basic way to test your math.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement