Using Java Random Class Using Java Math.Random

Let’s look at how to generate 10 random numbers in Java –

Example: Using Java Random Class

First, we will see the implementation using java.util.Random – Assume we need to generate 10 digit random number in Java between 0 to 100. Output: An object of Random class is initialized as objGenerator. The random number generator in Java class has a method as nextInt. This will provide a random number based on the argument specified as the upper limit, whereas it takes lower limit is 0.Thus, we get 10 random numbers displayed.

Example: Using Java Math.Random

Now, if we want 10 random numbers generated java but in the range of 0.0 to 1.0, then we should make use of random number generator Java class called math.random() . You can use the following loop to generate them- Output: Now, you know how those strange numbers are generated!!!

Summary:

Random number can be generated using two ways. You can use Random Java class (in package java.util) or Using Math.random Java class (however this will generate double in the range of 0.0 to 1.0 and not integers).