Java Random Number Generator
Learn how to generate random numbers in Java with interactive code examples. Explore Random.nextInt(), SecureRandom, ThreadLocalRandom, and Math.random() with detailed explanations.
Need a Quick Random Number?
Try our online random number generator for instant results without writing any code.
Java Code Editor
Learn how to generate random numbers in Java with interactive code examples. Explore Random.nextInt(), SecureRandom, ThreadLocalRandom, and Math.random() with detailed explanations.
Output
Simulated Output (Java runs on server)
Click "Run Code" to see example output
Basic Random with java.util.Random
Use Random.nextInt() to generate random integers in a specific range. This is the most common method for general-purpose random number generation in Java.
Understanding Java Random Number Generation
Java provides multiple ways to generate random numbers, each optimized for different use cases. java.util.Random uses a Linear Congruential Generator (LCG) algorithm, SecureRandom provides cryptographically strong random numbers, ThreadLocalRandom offers thread-safe generation for concurrent applications, and Math.random() provides a simple way to generate random doubles.
java.util.Random
java.util.Random provides fast pseudo-random number generation suitable for simulations, games, and general programming tasks. Thread-safe for basic use.
SecureRandom
java.security.SecureRandom uses platform-specific algorithms (SHA1PRNG, NativePRNG) to generate cryptographically secure random numbers for security applications.
ThreadLocalRandom
ThreadLocalRandom provides efficient thread-safe random number generation specifically designed for multi-threaded applications, avoiding contention and synchronization overhead.
Math.random()
Math.random() is a simple method that returns a double value between 0.0 and 1.0. Convenient for quick calculations but less flexible than other options.
Common Use Cases
Gaming & Simulations
Generate random dice rolls, card draws, enemy spawns, and game events using Random.nextInt() or ThreadLocalRandom in game loops.
Security & Cryptography
Create secure passwords, API keys, session tokens, and encryption salts using SecureRandom for cryptographic strength randomness.
Concurrent Applications
Use ThreadLocalRandom in parallel streams and multi-threaded applications to avoid contention and improve performance.
Software Testing
Create randomized test data, fuzz testing inputs, and edge cases to ensure robust code coverage and bug detection in Java applications.
How Java Random Number Generation Works
Java's java.util.Random class uses a Linear Congruential Generator (LCG) algorithm with a 48-bit seed. It provides a simple way to generate pseudo-random numbers and is synchronized for thread safety.
The SecureRandom class provides cryptographically strong random numbers by using platform-specific implementations. On most systems, it uses the operating system's entropy source or hardware random number generators.
ThreadLocalRandom, introduced in Java 7, is designed for multi-threaded applications. It uses a random number generator that is isolated to the current thread, avoiding synchronization overhead and contention.
Expert Tips for Java Random Generation
Choose the Right API
Use Random for simple cases, SecureRandom for security, ThreadLocalRandom for concurrent apps, and Math.random() only for quick prototypes. Each has specific use cases.
Always Use SecureRandom for Security
Never use Random or Math.random() for passwords, tokens, or cryptographic operations. Always use SecureRandom to ensure strong randomness and avoid predictable patterns.
Optimize for Concurrent Access
In multi-threaded applications, ThreadLocalRandom is significantly faster than synchronizing Random objects. Use it in parallel streams and concurrent collections.
Technical Specifications
Algorithms
- Linear Congruential Generator (Random class)
- SHA1PRNG or NativePRNG (SecureRandom)
- ThreadLocalRandom (Java 7+, based on Random)
- Pseudo-random double (Math.random())
Performance Characteristics
- Math.random(): Fastest for simple doubles
- ThreadLocalRandom: Best for concurrent access
- Random: Good general-purpose, synchronized overhead
- SecureRandom: Slower due to entropy gathering