Java Program To Generate Random Number Between 1 and 10

Java programming has almost all those features that can help to solve problems like- How to create a robust Java program that selects random numbers between 1 and 10 or any particular range?

Now after the release of Java 14 (on March 17, 2020) and Java 15 on (September 15, 2020), we have more classes and APIs to Generate random numbers between 1 and 10 in Java.

In previous tutorial, I’ve shared a detailed explaination on Random number generator in Java. Today we’ll see a Java Program To Generate Random Number Between 1 and 10 using one of the Random number generators of Java. So, let’s start the guide and take a deep dive into it –

Tutorial of Java-Random number between 1 and 10


Method used : Math.random()

The random() method is a static method of java.util.Math class. Also-

  • Math class is a final class, so no other class in Java inherits it,
  • The Math class contains rich methods for calculating square roots, trigonometric functions, basic numeric operations, and other regular calculations,
  • An essential point of this random-number generator method Math.random() is that, it can generate unpredictable random numbers in double type data formats only (between 0.0 and 1.0),
  • And one or multiple threads can use that method simultaneously, but take care of one thing. If multiple threads need to generate pseudo random numbers at a great rate, it will badly impact the performance of your program or method,

By the way, if you already been with core Java for a decent time and trying to level up your skills, drive into the Java web world and learn How to Start building scalable Java Microservices with Spring Boot and Spring Cloud by the Google cloud training team (so far 4.3/5 star and 43,960+ already enrolled) Coursera.

Example Program: Java Random Number Generation Between 1 and 10

// Program to generate 10 Random numbers in Java

public class GenerateRandomNumberInRange
{
    public static void main( String []arguments )
    {
        // Generating 10 Random Numbers
        for( int i=1; i<11; i++ )
            System.out.println("\nRandom Number : " + Math.random());
    }
}

 

Output:

If you will notice the below output of the above random number generator Java program, the data type of all the random numbers is of type double. Also, their range lies between zero (0.0) and one (1.0).

Random Number : 0.25752179370919814

Random Number : 0.9342551804940724

Random Number : 0.719818051969893

Random Number : 0.2751182748416443

Random Number : 0.10868437170930834

Random Number : 0.5664792643733297

Random Number : 0.5314561093013414

Random Number : 0.6773605404550109

Random Number : 0.12661061767238635

Random Number : 0.1522937549831539
Points to remember:
  • The method random() is a static method, so there is no need of creating an instance of java.util.Math class or any other Java class.
  • There may be some issues with its performance when multiple threads will generate random numbers at a great rate. It may misbehave at that time as this method is not thread safe.
  • So, if your program or application uses the multi-threading functionality, in that case, it’s better to use a thread-safe program.

Threads in Java, play a crucial role in increasing the performance of your app as well. In case, if you moving your head around android development, then Build Your First Android App (A Project-Centered Course) by Dr. Galtier is one of the best courses of android app development (so far, 4.2/5 stars and 1,706+ ratings by 277,730+ developers), where you can Enroll for free on Coursera.

Going to the Next Level: How to generate random number in Java using Math.random() method and some range:

// Program to generate random number between 1 to 10 using Math.random( )

public class FirstRandomNumberProgram
{
    public static void main( String []arguments )
    {
        // Assume the given lower range is 1
        int min = 1;
        // Assume the given upper range is 10
        int max = 10;
        // Assume we want 5 random numbers
        int totalRandomNumbers = 5;

        for(int k=0; k<totalRandomNumbers; k++)
        {   
            // Calculate the range of random number
            int range = (max - min) + 1;

            // Type Casting the double type random numbers into integer type
            int integerTypeRandomNumber = (int)((Math.random() * range) + min);
            System.out.println("Random Number(" +min+ "," +max+ ")= "+ integerTypeRandomNumber);
        }
    }
}

 

Output:

--------- Running First Time ------------
C:\Users\ShubhamKLogic_com\>javac FirstRandomNumberProgram.java
C:\Users\ShubhamKLogic_com\>java FirstRandomNumberProgram
Random Number(1,10)= 5
Random Number(1,10)= 4
Random Number(1,10)= 2
Random Number(1,10)= 4
Random Number(1,10)= 1

--------- Running Second Time ------------
C:\Users\ShubhamKLogic_com\>java FirstRandomNumberProgram
Random Number(1,10)= 3
Random Number(1,10)= 7
Random Number(1,10)= 1
Random Number(1,10)= 10
Random Number(1,10)= 10

--------- Running Third Time ------------
C:\Users\ShubhamKLogic_com\>java FirstRandomNumberProgram
Random Number(1,10)= 6
Random Number(1,10)= 1
Random Number(1,10)= 8
Random Number(1,10)= 1
Random Number(1,10)= 3

Conclusion 

In this tutorial, we have learned what does Math.random() method do, how to use it in a Java program to generate random number between 1 and 10.

We have developed multiple programs of Java random number between 1 and 10, and learned more about them. Now it’s your turn to copy the above code, paste in your editor and practice it, because- Practice it the king.

And, as always all the above code can be found over on Github.

Found Helpful?

Hope you found this tutorial helpful! What next? Rate the article on FB || 🎁Subscribe to the weekly News-letter || Connect with me on Twitter / Facebook / LinkedIn so that you will stay informed with Tech forever. Thank you for reading it, Best of Luck !


Leave a Comment