public class RandomTest{
 static Random random = new Random();
 
    static int r1(int n) {
        return (int)(Math.random() * n);
    }
    static int r2(int n) {
        return random.nextInt(n);
    }
 
 public static void main(String[] args) {
  long t1 = System.nanoTime();
  for (int i=0;i<10000;i++) {
   r1(1024);
  }
  long t2 = System.nanoTime();
  System.out.println(t2 - t1);
  t1 = System.nanoTime();
  for (int i=0;i<10000;i++) {
   r2(1024);
  }
  t2 = System.nanoTime();
  System.out.println(t2 - t1);
 }
}


?????????

????3422502
????1335365

???????????????????Ч???nextInt(n)????Math.random()??á?

?????????????

????Random???next(n)?????????????seed??????????nextseed?????seedλ48λ??bit????????????????????????????????????????


    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));
        return (int)(nextseed >>> (48 - bits));
    }