Developer Tools for Random Data Generation // v2.5.1
root@generate-random:~/numbers/java$ _

Generate Random Numbers in Java

Complete code tutorial with examples and best practices

[ Code Example - Quick Summary ]

Language: Java

What: Generate random numbers in Java using the <code>Random</code> class, <code>ThreadLocalRandom</code> for better performance, or <code>SecureRandom</code> for cryptographically secure random numbers.

Try it: Use our interactive Numbers generator or integrate this code into your Java application.

Generate random numbers in Java using the Random class, ThreadLocalRandom for better performance, or SecureRandom for cryptographically secure random numbers. Looking for other languages? Check our code examples in PHP , JavaScript , Python , C# , C++ , Ruby and Go or use our interactive web generator.

Java Code Example

import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import java.security.SecureRandom;
import java.util.stream.IntStream;

public class RandomNumberExample {
    public static void main(String[] args) {
        // Generate a single random number between 1 and 100
        Random random = new Random();
        int randomNumber = random.nextInt(100) + 1;
        System.out.println(randomNumber);  // Example output: 42

        // Generate multiple random numbers (Java 8+)
        int[] numbers = IntStream.generate(() ->
            ThreadLocalRandom.current().nextInt(1, 101)
        ).limit(10).toArray();

        System.out.println(java.util.Arrays.toString(numbers));
        // Example output: [23, 7, 91, 45, 12, 68, 3, 89, 54, 31]

        // Generate cryptographically secure random number
        SecureRandom secureRandom = new SecureRandom();
        int secureNumber = secureRandom.nextInt(100) + 1;
        System.out.println(secureNumber);  // Example: 67

        // Random double between 0 and 100
        double randomDouble = random.nextDouble() * 100;
        System.out.printf("%.2f%n", randomDouble);  // Example: 42.17
    }
}

[EXPLANATION]

Random.nextInt(n) returns a value from 0 (inclusive) to n (exclusive), so add your minimum value. ThreadLocalRandom is preferred for concurrent applications as it reduces contention. SecureRandom provides cryptographically strong random numbers but is slower.

Expected Output

42
[23, 7, 91, 45, 12, 68, 3, 89, 54, 31]
67
42.17

Common Use Cases

  • Enterprise application random data generation
  • Android app development (random features)
  • Cryptographic token and key generation with SecureRandom
  • Multi-threaded applications using ThreadLocalRandom
  • Game development and simulation engines
  • JUnit test data generation

Important Notes

  • Use ThreadLocalRandom for better performance in concurrent code
  • SecureRandom is thread-safe but slower
  • Java 8+ streams provide elegant syntax for multiple randoms
  • Random can be seeded for reproducible results

Try Our Interactive Generator

Don't want to write code? Use our free web-based Numbers generator with instant results.

TRY NUMBERS GENERATOR →