This write-up, will acknowledge us about generating random numbers in Java using the following approaches
- Math.random()
- ThreadLocalRandom Class
- Random Class
Method 1: Use Math.random() method
In Java, random numbers can be generated by using the Math.random() method. It generates random positive double data type values. This generates numbers between 0.0 and 1.0.
Code:
{
public static void main(String[] args)
{
for(int m=1;m<=5;m++)
{
System.out.println(Math.random());
}
}
}
In this code we create a main function.Then inside the main function, we create a for loop and inside the for loop, we use the Math.random() method in order to generate and display a random number every time the loop executes itself.
Output:
In this output, we can clearly see that every time the loop executes a random number is generated between 0.0 and 1.0.
Method 2: Use ThreadLocalRandom Class
We can also use the ThreadLocalRandom class to generate random numbers. It generates the random numbers of integers, doubles, booleans, etc. This Class is a part of the java.util.concurrent package. Now let’s see how we generate random numbers using this class in the following example.
Code:
public class ran
{
public static void main(String[] args)
{
for(int m=1;m<=3;m++)
{
System.out.println(ThreadLocalRandom.current().nextDouble());
System.out.println(ThreadLocalRandom.current().nextInt());
System.out.println(ThreadLocalRandom.current().nextBoolean());
System.out.println(" ");
}
}
}
In this code, we create a for loop inside the main function. Then we use the ThreadLocalRandom Class to generate a random number of Double, Integer, and Boolean types by invoking nextInt, nextBoolean and nextDouble methods with it.
Output:
The output shows that the loop executes three times and every time it generates a random number of double, integer and boolean data types.
Method 3: Use Random Class
This class is present in the java.util package. This class generates a random number of Integer, Double, Long and boolean data types. In order to use this class to generate random numbers we need to create an object for the class and then invoke nextInt, nextLong, nextDouble and nextBoolean methods in it. In this class we can also pass the range as an argument.
Code:
import java.util.Random;
public class ran
{
public static void main(String[] args)
{
Random rand = new Random();
for(int m=1;m<=3;m++)
{
System.out.println(ran.nextInt(50));
System.out.println(ran.nextDouble(40));
System.out.println(ran.nextLong(30));
System.out.println(" ");
}
}
}
In this code, we create a ran object for Random class in order to access its methods and then we invoke it with nextInt, nextDouble and nextLong with arguments as the range to generate a random number within the given range.
Output:
In this output, we can see that random numbers are generated between 50, 40 and 30 every time the loop executes.
Conclusion
In Java, random numbers are generated using the Math.random method, ThreadLocalRandom Class and java.util.Random Class. In this article, we have talked about the approaches of generating random numbers in Java in detail with examples. This article is fully packed with all the necessary information you need to learn about generating a random number in Java.