Generate Random Numbers using random method of Java Math class

This example shows how to generate random numbers using the random method of the Java Math class including generating random numbers between a specific range.

How to generate random numbers in Java?

Use the random static method of the Math class to generate random numbers in Java.

This method returns a double number greater than or equal to 0.0 and less than 1.0 (Note that the 0.0 is inclusive while 1.0 is exclusive so that 0 <= n < 1)

a) How to generate a random number between 0 and 1?

Output (could be different for you as these are random numbers)

b) Between 0 and 100

It is fairly easy task to generate random numbers between 0 and 100. Since random() method returns a number between 0.0 and 1.0, multiplying it with 100 and casting the result to an integer will give us a random number between 0 and 100 (where 0 is inclusive while 100 is exclusive).

c) Between a specific range

Since the random method returns a double value between 0.0 and 1.0, we need to derive a formula so that we can generate numbers in the specific range.

Let’s do that step by step. Suppose you want to generate random numbers between 10 and 20. So the minimum number it should generate is 10 and the maximum number should be 20.

Step 1:

First of all, we need to multiply the random method result with the maximum number so that it returns value between 0 to max value (in this case 20) like given below.

The above statement will return us a random number between 0.0 and 19. That is because multiplying 0.0 – 0.99 with 20 and casting the result back to int will give us range of the 0 to 19.

Step 2:

Step 1 gives us a random number between 0 and 19. But we want a random number starting from 10, not 0. Let’s add that number to the result.

Step 3:

Now the number starts from 10 but it goes up to 30. That is because adding 10 to 0-19 will give us 10-29. So let’s subtract 10 from 20 before multiplication operation.

Step 4:

The random number generated by the above formula gives us a range between 10 and 19 (both inclusive). The number range we wanted was between 10 and 20 (both inclusive). So let’s add 1 to the equation.

A final result is a random number in the range of 10 to 20.

The general formula to generate random numbers in a specific range

Formula to generate random number in the range of min and max is as given below.

The final example to generate random numbers in a specific range is given below.

This example is a part of the Java Math class tutorial with examples.

Please let me know your views in the comments section below.

About the author



My name is RahimV and I have over 16 years of experience in designing and developing Java applications. Over the years I have worked with many fortune 500 companies as an eCommerce Architect. My goal is to provide high quality but simple to understand Java tutorials and examples for free. If you like my website, follow me on Facebook and Twitter.

Add Comment

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

Online Shopping