Generate random numbers in Java
In this post, you will learn different ways to find a random number in the Java programming language.
Such a type of logical question is generally asked in an interview or in competitive exams. This can be helpful in improving your logical programming skills and is helpful in game development and other logical application development.
There are different ways to generate random numbers in Java. Here, we have mentioned most of them.
Using random class
Java Random class is used to generate a stream of pseudorandom numbers. We can use this to generate a random number of any data type, such as integer, float, double, Boolean, long. These are the following steps to use the Random Class to generate random numbers.
- Import the class java.util.Random
- Create the instance of the class Random, i.e., Random rand = new Random()
- Call any one of the following methods of the rand object -
- nextInt(int bound)
- nextInt()
- nextFloat()
- nextDouble()
- nextLong()
- nextBoolean()
The given code generates the random numbers using the random class.
import java.util.Random;
public class GenerateRandom {
public static void main( String args[] ) {
//Instance of random class
Random rand = new Random();
int max = 20;
//generate random values
int int_random = rand.nextInt(max);
double double_random=rand.nextDouble();
float float_random=rand.nextFloat();
System.out.println("Random integer value from 0 to " + (max-1) + " : "+ int_random);
System.out.println("Random float value between 0.0 and 1.0 : "+float_random);
System.out.println("Random double value between 0.0 and 1.0 : "+double_random);
}
}
Output of the above code:
Random integer value from 0 to 19 : 8
Random float value between 0.0 and 1.0 : 0.42801195
Random double value between 0.0 and 1.0 : 0.9829409679874449
Using Math.random
The Math.random() method returns a pseudorandom number, but in the range of 0.0 to 1.0.. It is a static method of the Math class.
import java.util.*;
public class generateRandom
{
public static void main(String args[])
{
// Generating random numbers
System.out.println("Random doubles: " + Math.random());
System.out.println("Random doubles: " + Math.random());
}
}
Output of the above code:
Random doubles: 0.4229693418207243
Random doubles: 0.2312890568633339
Using java.util.concurrent.ThreadLocalRandom class
ThreadLocalRandom is a combination of the ThreadLocal and Random classes and is isolated to the current thread. These are the following steps to generate random numbers using the class ThreadLocalRandom.
- Import the class java.util.concurrent.ThreadLocalRandom
- Call any one of the following methods -
- ThreadLocalRandom.current().nextInt() - To generate random number of type int.
- ThreadLocalRandom.current().nextDouble() - To generate random number of type double.
- ThreadLocalRandom.current().nextBoolean() - To generate random number of type boolean.
import java.util.concurrent.ThreadLocalRandom;
public class GenerateRandom {
public static void main( String args[] ) {
// Generate random integers
int int_rand = ThreadLocalRandom.current().nextInt();
System.out.println("Random Integers: " + int_rand);
// Generate Random doubles
double double_rand = ThreadLocalRandom.current().nextDouble();
System.out.println("Random Doubles: " + double_rand);
// Generate random booleans
boolean boolean_rand = ThreadLocalRandom.current().nextBoolean();
System.out.println("Random Booleans: " + boolean_rand);
}
}
Output of the above code:
Random Integers: -487168187
Random Doubles: 0.4980937515356161
Random Booleans: false
Related Articles
Sort array in ascending order JavaAutomorphic number in Java
Pascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java