In this tutorial, we will learn about Generate Random Numbers-
- Using Java Random Class
- Using Java Math.Random
Random number can be generated using two ways. java.util.Random class is used to generate random numbers of different data types such as boolean, int, long, float, and double. An object of Random class is initialized and the method nextInt(), nextDouble() or nextLong() is used to generate random number. You can also use Math.Random to generate random value between 0.0 and 1.0.
Let's look at them one by one -
Example: Using Java Random Class
First, we will see the implementation using java.util.Random - Assume we need to generate 10 random numbers between 0 to 100.
Program:
import java.util.Random;
public class RandomNumbers
{
public static void main(String[] args)
{
Random objGenerator = new Random();
for (int iCount = 0; iCount< 10; iCount++)
{
int randomNumber = objGenerator.nextInt(100);
System.out.println("Random No : " + randomNumber);
}
}
}
public class RandomNumbers
{
public static void main(String[] args)
{
Random objGenerator = new Random();
for (int iCount = 0; iCount< 10; iCount++)
{
int randomNumber = objGenerator.nextInt(100);
System.out.println("Random No : " + randomNumber);
}
}
}
Output:
Random No : 5
Random No : 26
Random No : 97
Random No : 96
Random No : 51
Random No : 49
Random No : 75
Random No : 12
Random No : 73
Random No : 54
Random No : 26
Random No : 97
Random No : 96
Random No : 51
Random No : 49
Random No : 75
Random No : 12
Random No : 73
Random No : 54
An object of Random class is initialized as objGenerator. The Random 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 math.random().
You can use the following loop to generate them-
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 math.random().
You can use the following loop to generate them-
public class DemoRandom
{
public static void main(String[] args)
{
for(int xCount = 0; xCount< 10; xCount++)
{
System.out.println(Math.random());
}
}
}
{
public static void main(String[] args)
{
for(int xCount = 0; xCount< 10; xCount++)
{
System.out.println(Math.random());
}
}
}
Output:
0.5988432594227765
0.6726200524985211
0.15623043893786048
0.5992563534783838
0.512766807457192
0.45942970665891725
0.3640623175839254
0.2905739923111288
0.6482129633289565
0.07637537439495412
0.6726200524985211
0.15623043893786048
0.5992563534783838
0.512766807457192
0.45942970665891725
0.3640623175839254
0.2905739923111288
0.6482129633289565
0.07637537439495412
Now, you know how those strange numbers are generated!!!
Summary:
Random number can be generated using two ways. You can use Random 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).
Random number can be generated using two ways. You can use Random 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).