In Java, Random and SecureRandom are used to generate random numbers. While both produce random values, they serve very different purposes in real-world applications.
Click "Generate" to see how they differ in security context.
Randomjava.util.Random uses a linear congruential algorithmjava.security.SecureRandom uses OS-level entropy sources
// Generating random numbers using java.util.Random
import java.util.Random;
public class RandomDemo {
public static void main(String[] args) {
Random random = new Random();
int number = random.nextInt(100);
System.out.println(number);
}
}
// Generating secure random numbers using SecureRandom
import java.security.SecureRandom;
public class SecureRandomDemo {
public static void main(String[] args) {
SecureRandom secureRandom = new SecureRandom();
int number = secureRandom.nextInt(100);
System.out.println(number);
}
}
Each program prints a random number between 0 and 99. The difference is internal — SecureRandom ensures unpredictability suitable for security.
Random for games, simulations, and testingSecureRandom for passwords, tokens, and encryption keysRandom in authentication systems