Generating Random Numbers with Random Class
To generate a random integer:
int randomInt = random.nextInt();
To generate a random integer within a specific range (e.g., 0 to 99):
int randomIntInRange = random.nextInt(100);
Example
Java
import java.util.Random;
public class RandomExample {
public static void main(String[] args) {
Random random = new Random();
double randomDouble = random.nextDouble();
System.out.println("Random Double: " + randomDouble);
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Random Double: 0.4644784159902743
Using Math.random() Method
The Math.random() method returns a double value between 0.0 (inclusive) and 1.0 (exclusive). It's a simple way to generate random numbers without creating an instance of any class.
Syntax of Math.random()
double randomValue = Math.random();
Generating Random Numbers with Math.random()
To generate a random double:
double randomDouble = Math.random();
Generating Random Integers within a Range
To generate a random integer within a specific range (e.g., 0 to 99):
int randomIntInRange = (int) (Math.random() * 100);
Examples with Math.random()
Example 1: Generating Random Double
Java
public class MathRandomExample {
public static void main(String[] args) {
double randomDouble = Math.random();
System.out.println("Random Double: " + randomDouble);
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Random Double: 0.9651842982270894
Example 2: Generating Random Integer within a Range
Java
public class MathRandomExample {
public static void main(String[] args) {
int randomInt = (int) (Math.random() * 100);
System.out.println("Random Integer: " + randomInt);
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Random Integer: 43
Using ThreadLocalRandom Class
The ThreadLocalRandom class is designed for use in multi-threaded environments. It's more efficient than Random when used within a single thread.
Syntax of ThreadLocalRandom
To use ThreadLocalRandom, you can call its static methods:
import java.util.concurrent.ThreadLocalRandom;
int randomInt = ThreadLocalRandom.current().nextInt();
Generating Random Numbers with ThreadLocalRandom
To generate a random integer:
int randomInt = ThreadLocalRandom.current().nextInt();
Example
Java
import java.util.concurrent.ThreadLocalRandom;
public class ThreadLocalRandomExample {
public static void main(String[] args) {
int randomInt = ThreadLocalRandom.current().nextInt();
System.out.println("Random Integer: " + randomInt);
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Random Integer: 870004142
Frequently Asked Questions
What is the difference between Random and SecureRandom?
Random is suitable for general-purpose use, while SecureRandom provides cryptographically strong random numbers for security-sensitive applications.
How do I generate random numbers in a specific range?
Use nextInt(bound) with Random or ThreadLocalRandom, and (int) (Math.random() * range) with Math.random().
Can Math.random() be used for secure random number generation?
No, Math.random() is not suitable for security-sensitive applications. Use SecureRandom instead.
Conclusion
Generating random numbers in Java is a versatile task that can be accomplished using various classes and methods. Whether you need simple random numbers for a game or secure random numbers for encryption, Java provides the tools you need. By understanding and applying these methods, you can effectively generate random numbers for your applications.
You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.