package mypackage;
import java.security.SecureRandom;
import java.util.Random;
public class SecureRandomTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		SecureRandom sr1 = new SecureRandom();
		System.out.println("1. SecureRandom object 1. nextInt() result :" + sr1.nextInt());
		System.out.println("1. SecureRandom object 2. nextInt() result :" + sr1.nextInt());
		System.out.println("1. SecureRandom object 3. nextInt() result :" + sr1.nextInt());
		System.out.println("1. SecureRandom object 4. nextInt() result :" + sr1.nextInt());
		System.out.println("1. SecureRandom object 5. nextInt() result :" + sr1.nextInt()+"\n\n\n");
		SecureRandom sr2 = new SecureRandom();
		System.out.println("2. SecureRandom object 1.nextInt() result :" + sr2.nextInt()+"\n\n\n");
		Random r1 = new Random();
		System.out.println("1. Random object 1. nextInt() result :" + r1.nextInt());
		System.out.println("1. Random object 2. nextInt() result :" + r1.nextInt()+"\n\n\n");
		Random r2 = new Random();
		System.out.println("2. Random object 1. nextInt() result :" + r2.nextInt());
	}

}

The output is :

1. SecureRandom object 1. nextInt() result :2081492090
1. SecureRandom object 2. nextInt() result :1073396477
1. SecureRandom object 3. nextInt() result :134130488
1. SecureRandom object 4. nextInt() result :-1229020596
1. SecureRandom object 5. nextInt() result :-679432773

2. SecureRandom object 1.nextInt() result :1924631447

1. Random object 1. nextInt() result :1121875770
1. Random object 2. nextInt() result :-486804200

2. Random object 1. nextInt() result :1910565042


So what is the difference? I can't see any difference between these two...

Dani AI

Generated

Short answer: they both produce pseudorandom numbers, but they’re built for different goals. ’s output looks “the same” because both return int-sized, random-looking values, but java.util.Random is a fast, deterministic PRNG (good for simulations, games, tests) while java.security.SecureRandom is designed to be cryptographically strong and unpredictable (use it for keys, nonces, session IDs, salts). was right to point at the API, but here’s a plain-language, practical comparison.

Why your sample prints look similar — and a quick demo:

Random r1 = new Random(12345L);
Random r2 = new Random(12345L);
System.out.println(r1.nextInt() == r2.nextInt()); // true

SecureRandom s1 = new SecureRandom();
s1.setSeed(12345L);
SecureRandom s2 = new SecureRandom();
s2.setSeed(12345L);
System.out.println(s1.nextInt() == s2.nextInt()); // often false; reproducibility not guaranteed

Random seeded with the same seed gives identical sequences. SecureRandom usually mixes OS entropy and provider state, so seeding does not guarantee identical streams.

Practical guidance and cautions: use Random (or ThreadLocalRandom/SplittableRandom for concurrent/high-performance non-crypto work) for simulations and non-security tasks. Use SecureRandom for anything an attacker could exploit (key generation, salts, tokens). Never seed security values with System.currentTimeMillis and avoid ad-hoc bounded reductions like Math.abs(r.nextInt()) % n (use nextInt(bound) instead to avoid bias).

Quick tips: generate tokens with SecureRandom.nextBytes(...) and encode with a URL-safe Base64. If SecureRandom appears to block at startup, it’s usually waiting on OS entropy; on servers you may prefer a non-blocking source or configure the JVM entropy source appropriately.

Recommended Answers

All 2 Replies

Isn't API explanation of Random and SecureRandom enough for you?

Isn't API explanation of Random and SecureRandom enough for you?

Sadly no :(

I'm sorry for asking such a question like this. But i wanted a clear explanation from a person who has good and deep knowledge about this topic.

Sorry again for asking :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.