java 随机数种子

引子:需要实现每天随机获得一个礼包,且全服玩家随出来的都是同一个。

实现方案:以当前时间是一年的第几天作为random的种子,取1~礼包总个数范围内的随机值。

  public static int getBuffId() {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(System.nanoTime());
        int dateInYear = c.get(Calendar.DAY_OF_YEAR);
        return new Random(dateInYear).nextInt(10) + 1;
    }

于是决定深入了解一下随机数的种子。

Random两种构造方法。一种是无参构造函数。种子是 ++seedUniquifier + System.nanoTime(), 所以每新new一个Random实例,种子都会变。

private static volatile long seedUniquifier = 8682522807148012L;
public Random() { this(++seedUniquifier + System.nanoTime()); }

另一种是设置种子的构造方法。

 public Random(long seed) {
        this.seed = new AtomicLong(0L);
        setSeed(seed);
    }

看一下next()方法。每次调用next()方法。种子就会发生变化  nextseed = (oldseed * multiplier + addend) & mask;

private final static long multiplier = 0x5DEECE66DL;
private final static long addend = 0xBL;
private final static long mask = (1L << 48) - 1;
protected int next(int bits) { long oldseed, nextseed; AtomicLong seed = this.seed; do { oldseed = seed.get(); nextseed = (oldseed * multiplier + addend) & mask; } while (!seed.compareAndSet(oldseed, nextseed));//保证oldseed跟nextseed不相同 return (int)(nextseed >>> (48 - bits)); }
public int nextInt() { return next(32); }

实例:

//实例1
       Random random = new Random(1000);
        for (int i = 1; i < 4; i++) {
            System.out.println(random.nextInt());
        }
   
        System.out.println("...........");

//实例2
        for (int j = 0; j < 4; j++) {
            System.out.println(new Random(1000).nextInt());
        }

运行结果:

1487836800000
-1244746321
1060493871
-1826063944
...........
-1244746321
-1244746321
-1244746321
-1244746321

分析:运行结果不一样。原因如下:

        实例1设定了种子1000,for循环里,nextSeed一直在变,所以随出来的值也每次不一样。

        实例2每次new一个新的Random对象,每次seed都会被重置成1000,后面调用nextInt() 算出来的nextSeed都是同一个值,所以不管循环几次,随出来的结果都一样。

原文地址:https://www.cnblogs.com/happySmily/p/6428553.html