Generate random number

Using Math.random works when you need to generate a single random number. If you need a series of random numbers, you should create an instance of java.util.Random and invoke methods on that object to generate numbers. In Java 8, java.util.Random is enhanced to return an effectively unlimited stream by passing origin and bound parameters.

Straight up Java

Random number

@Test
public void generate_random_number_with_java () {
    Random random = new Random();

    double randomNumber = random.nextInt(10);

    assertTrue(randomNumber <=10);
}

Secure random number

@Test
public void generate_secure_random_number_with_java () {
    SecureRandom random = new SecureRandom();

    double randomNumber = random.nextInt(10);

    assertTrue(randomNumber <=10);
}

Random number Math.random()

@Test
public void generate_single_random_number () {

    int randomNumber = (int)(Math.random() * 10);

    assertTrue(randomNumber < 10);
}

Java 8

@Test
public void generate_random_number_java8 () {

    Random random = new Random(10);

    OptionalInt randomNumber = random.ints(0, 10).findFirst();

    assertTrue(randomNumber.getAsInt() < 10);
}