for (int i = 0; i < 2; i++) {
System.out.println(Thread.currentThread().getName() + ": " + Math.random());
}
}
}
结果:
Thread-1: 0.8043581595645333 Thread-0: 0.9338269554390357 Thread-1: 0.5571569413128877 Thread-0: 0.37484586843392464
2.java.util.Random 工具类
基本算法:linear congruential pseudorandom number generator (LGC) 线性同余法伪随机数生成器缺点:可预测
An attacker will simply compute the seed from the output values observed. This takes significantly less time than 2^48 in the case of java.util.Random. 从输出中可以很容易计算出种子值。It is shown that you can predict future Random outputs observing only two(!) output values in time roughly 2^16. 因此可以预测出下一个输出的随机数。You should never use an LCG for security-critical purposes.在注重信息安全的应用中,不要使用 LCG 算法生成随机数,请使用 SecureRandom。
使用:
Random random = new Random();
for (int i = 0; i < 5; i++) {
System.out.println(random.nextInt());
}
结果:
-24520987 -96094681 -952622427 300260419 1489256498
Random类默认使用当前系统时钟作为种子:
public Random() {
this(seedUniquifier() ^ System.nanoTime());
}
public Random(long seed) {
if (getClass() == Random.class)
this.seed = new AtomicLong(initialScramble(seed));
else {
// subclass might have overriden setSeed
this.seed = new AtomicLong();
setSeed(seed);
}
}
Random类提供的方法:API
- nextBoolean() - 返回均匀分布的 true 或者 false
- nextBytes(byte[] bytes)
- nextDouble() - 返回 0.0 到 1.0 之间的均匀分布的 double
- nextFloat() - 返回 0.0 到 1.0 之间的均匀分布的 float
- nextGaussian()- 返回 0.0 到 1.0 之间的高斯分布(即正态分布)的 double
- nextInt() - 返回均匀分布的 int
- nextInt(int n) - 返回 0 到 n 之间的均匀分布的 int (包括 0,不包括 n)
- nextLong() - 返回均匀分布的 long
- setSeed(long seed) - 设置种子
只要种子一样,产生的随机数也一样: 因为种子确定,随机数算法也确定,因此输出是确定的!
Random random1 = new Random(10000);
Random random2 = new Random(10000);
for (int i = 0; i < 5; i++) {
System.out.println(random1.nextInt() + " = " + random2.nextInt());
}
结果:
-498702880 = -498702880 -858606152 = -858606152 1942818232 = 1942818232 -1044940345 = -1044940345 1588429001 = 1588429001
3.java.util.concurrent.ThreadLocalRandom 工具类
ThreadLocalRandom 是 JDK 7 之后提供,也是继承至 java.util.Random。
private static final ThreadLocal localRandom =
new ThreadLocal() {
protected ThreadLocalRandom initialValue() {
return new ThreadLocalRandom();
}
};
每一个线程有一个独立的随机数生成器,用于并发产生随机数,能够解决多个线程发生的竞争争夺。效率更高!关注公众号Java技术栈回复 java 获取更多 Java 工具类教程。
ThreadLocalRandom 不是直接用 new 实例化,而是第一次使用其静态方法 current() 得到 ThreadLocal 实例,然后调用 java.util.Random 类提供的方法获得各种随机数。
使用:
public class JavaRandom {
public static void main(String args[]) {
new MyThread().start();
new MyThread().start();
}
}
class MyThread extends Thread {
public void run() {
for (int i = 0; i < 2; i++) {
System.out.println(Thread.currentThread().getName() + ": " + ThreadLocalRandom.current().nextDouble());
}
}
}
结果:
Thread-0: 0.13267085355389086 Thread-1: 0.1138484950410098 Thread-0: 0.17187774671469858 Thread-1: 0.9305225910262372
4.java.Security.SecureRandom
也是继承至 java.util.Random。
Instances of java.util.Random are not cryptographically secure. Consider instead using SecureRandom to get a cryptographically secure pseudo-random number generator for use by security-sensitive applications.SecureRandom takes Random Data from your os (they can be interval between keystrokes etc - most os collect these data store them in files - /dev/random and /dev/urandom in case of linux/solaris) and uses that as the seed. 操作系统收集了一些随机事件,比如鼠标点击,键盘点击等等,SecureRandom 使用这些随机事件作为种子。
SecureRandom 提供加密的强随机数生成器 (RNG),要求种子必须是不可预知的,产生非确定性输出。SecureRandom 也提供了与实现无关的算法,因此,调用方(应用程序代码)会请求特定的 RNG 算法并将它传回到该算法的 SecureRandom 对象中。
- 如果仅指定算法名称,如下所示:SecureRandom random = SecureRandom.getInstance(“SHA1PRNG”);
- 如果既指定了算法名称又指定了包提供程序,如下所示:SecureRandom random = SecureRandom.getInstance(“SHA1PRNG”, “SUN”);
使用: