DEV Community

Edem Agbenyo
Edem Agbenyo

Posted on • Updated on

100 Days of Java: Day 1

Day 001 - Generating a Random Number in Java

Welcome to Day 001 of the 100 Days of Code challenge! In this series, we will explore different Java code snippets and explain them in simple terms. Today, we will dive into generating a random number within a specific range using Java.

import java.security.SecureRandom;

public final class Day001 {

    public static final SecureRandom SECURE_RANDOM = new SecureRandom();

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

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

}
Enter fullscreen mode Exit fullscreen mode

Code Explanation:

Let's break down the above code snippet and understand how it works step by step:

Importing Required Classes:

The code begins by importing the necessary class, SecureRandom, from the java.security package. This class provides a cryptographically strong random number generator.

Defining the Day001 Class:

Next, we define a class named Day001. The public final keywords indicate that this class cannot be extended or subclassed.

Creating a SecureRandom Object:

Inside the Day001 class, we declare a public constant variable called SECURE_RANDOM, which is an instance of the SecureRandom class. This variable will be used to generate random numbers securely.

The main() Method:

The main method is the entry point of the program. When the code is executed, this method is called automatically. It takes an array of strings as its argument, although we are not using it in this example.

Printing a Message:

Within the main method, we print the message "Generating a random number between 100 and 200" using the System.out.println() method. This message indicates the range within which our random number will be generated.

Generating the Random Number:

After printing the message, we call the randomNumber() method and pass the minimum and maximum values (100 and 200) as arguments.

The randomNumber() Method:

The randomNumber() method is a private helper method we defined that takes two integers, as input. It generates a random number within the specified range and returns it as the result.

Calculating the Random Number:

This is where the real magic happens. Inside the randomNumber() method, we use the nextInt() method of the SecureRandom class to generate a random integer. We calculate the range of the random number by subtracting the minimum from the maximum. Then, we add the minimum value to the result to ensure the number falls within the desired range.

Printing the Random Number:
Back in the main method, we print the generated random number using the System.out.println() method.

Today, we learned how to generate a random number within a specific range using Java. By utilizing the SecureRandom class, we ensured that the generated numbers are cryptographically strong. Feel free to modify the range and experiment with different values to generate random numbers for your own projects.
Congratulations on completing Day 001 of the 100 Days of Code challenge!

Stay tuned for Day 002, where we will explore another exciting Java code snippet! Happy coding!

Top comments (0)