Home » Generating a random number within a specific range

Generating a random number within a specific range

cube-six-gambling-play-37534.jpeg

Here’s how to generate a random number within a specific range in Java.

import java.security.SecureRandom;

public final class RandomNumber {

    public static final SecureRandom SECURE_RANDOM = new SecureRandom();

    public static void main(String[] args) {
        System.out.println("Generating a number between 50 and 100...");
        System.out.println(randomNumberBetween(50, 100));
    }

    private static int randomNumberBetween(int minimum, int maximum) {
        return SECURE_RANDOM.nextInt(maximum - minimum) + minimum;
    }

}

If you have any questions, leave a comment or ask me on my social media.

Leave a Reply

Your email address will not be published. Required fields are marked *