Java program to generate a random number between 1 and 100 – Solution

Java program to generate a random number between 1 and 100. Generating a random number between 1 and 100 in Java can be easily accomplished using the java.util.Random class or the Math.random() method. Below is an example of how you can create a Java program to generate a random number in this range using both methods.

Java Program to Generate a Random Number Between 1 and 100

Java program to generate a random number between 1 and 100

Using java.util.Random

The Random class provides methods to generate random numbers of various types, including integers within a specified range.

import java.util.Random;

public class RandomNumberGenerator {
    public static void main(String[] args) {
        // Create an instance of the Random class
        Random random = new Random();

        // Generate a random number between 1 and 100
        int randomNumber = random.nextInt(100) + 1;

        // Print the random number
        System.out.println("Random number between 1 and 100: " + randomNumber);
    }
}

Explanation of the Code

Let’s break down the program step-by-step to understand how it works.

  1. Importing the Random Class: We import the Random class from the java.util package to generate random numbers.
import java.util.Random;

2. Creating the Main Class: We create a public class named RandomNumberGenerator. In Java, the class name should match the filename.

public class RandomNumberGenerator {

3. Main Method: The main method is the entry point of the program. Inside this method, we will write the code to generate a random number.

public static void main(String[] args) {

4. Creating an Instance of the Random Class: We create an instance of the Random class to generate random numbers.

Random random = new Random();

5. Generating a Random Number: We use the nextInt method of the Random class to generate a random integer. The argument 100 specifies the upper bound (exclusive), so the range is from 0 to 99. Adding 1 shifts the range to 1 to 100.

int randomNumber = random.nextInt(100) + 1;

6. Printing the Random Number: We print the generated random number.

System.out.println("Random number between 1 and 100: " + randomNumber);

Running the Program

To run the program, follow these steps:

  1. Save the code in a file named RandomNumberGenerator.java.
  2. Open a command prompt or terminal and navigate to the directory where you saved the file.
  3. Compile the program using the following command:
javac RandomNumberGenerator.java

4. Run the compiled program using the following command:

java RandomNumberGenerator

Are you looking for a job: Click Here

Top 100 Java Programs: Click Here

Example Output (Java program to generate a random number between 1 and 100)

Each time you run the program, you will get a different random number between 1 and 100. For example:

Random number between 1 and 100: 57
Random number between 1 and 100: 34

Alternative Approach Using Math.random()

Another way to generate a random number in Java is by using the Math.random() method. This method returns a double value between 0.0 (inclusive) and 1.0 (exclusive). By scaling and shifting this value, we can obtain an integer in the desired range.

public class RandomNumberGeneratorMath {
    public static void main(String[] args) {
        // Generate a random number between 1 and 100
        int randomNumber = (int) (Math.random() * 100) + 1;

        // Print the random number
        System.out.println("Random number between 1 and 100: " + randomNumber);
    }
}

Explanation of the Code

  1. Main Class and Method: Similar to the previous example, we create a public class named RandomNumberGeneratorMath and define the main method.
public class RandomNumberGeneratorMath {
    public static void main(String[] args) {

2. Generating a Random Number: We use the Math.random() method to generate a random double value between 0.0 and 1.0. By multiplying this value by 100, we obtain a range from 0.0 to 99.9999. Casting the result to int truncates the decimal part, resulting in an integer range from 0 to 99. Adding 1 shifts the range to 1 to 100.

int randomNumber = (int) (Math.random() * 100) + 1;

3 . Printing the Random Number: We print the generated random number.

System.out.println("Random number between 1 and 100: " + randomNumber);

What did we learn from this article?

Generating a random number between 1 and 100 in Java can be easily accomplished using either the Random class or the Math.random() method. Both approaches are simple and effective, providing flexibility in how you choose to implement random number generation in your programs. By practicing these basic techniques, you can enhance your understanding of Java’s capabilities and improve your problem-solving skills in programming. Keep experimenting with different methods and explore more advanced topics in random number generation to broaden your knowledge and skills.

Java program to generate a random number between 1 and 100.

Leave a Comment