java.util.random如何工作?

时间:2017-11-10 19:15:56

标签: java random lcg

为了理解java.util.random是如何工作的,我编写了一段简单的代码来模拟java随机函数,并比较了java随机函数和我的函数的结果。但是,结果不同。这意味着要么我犯了一些错误,要么误解了这个概念。

import java.util.Random;

public class test2 {
  private static long multiplier = 0x5DEECE66DL;

  private static long addend = 0xBL;

  private static long mask = (1L << 48) - 1;


  public static void main(String args[]){
    long seed = 128856;
    Random random = new Random(seed);
    long n1 = random.nextInt();
    long n2 = random.nextInt();
    long n3 = random.nextInt();

    System.out.println("Results: " + n1 +" "+ n2 +" "+ n3);


    System.out.println("seed: " + seed);
    long seed0 = (seed ^ multiplier) & mask;
    System.out.println("seed0: " + seed0);

    long seed1 = ((seed0 * multiplier + addend) & mask);
    System.out.println("seed1: " + seed1);     
    long v1 = seed1 >>> 16;
    System.out.println("v1: " + v1);

    long seed2 = ((seed1 * multiplier + addend) & mask); 
    System.out.println("seed2: " + seed2);
    long v2 = seed2 >>> 16;
    System.out.println("v2: " + v2);
  }   

}

以下是结果的屏幕截图: Result

n1不等于v1。请告诉我我犯的错误是什么?谢谢。

1 个答案:

答案 0 :(得分:0)

好问题!随机生成器毕竟不是随机生成器!您的代与Random之间的唯一区别是您返回,而Random将其转换为 int

以下更改将解决此问题:

public static void main(String args[]){
     long multiplier = 0x5DEECE66DL;

    long addend = 0xBL;

     long mask = (1L << 48) - 1;

    long seed = 128856;
    Random random = new Random(seed);
    long n1 = random.nextInt();
    long n2 = random.nextInt();
    long n3 = random.nextInt();

    System.out.println("Results: " + n1 +" "+ n2 +" "+ n3);


    System.out.println("seed: " + seed);
    long seed0 = (seed ^ multiplier) & mask;
    System.out.println("seed0: " + seed0);

    long seed1 = ((seed0 * multiplier + addend) & mask);
    System.out.println("seed1: " + seed1);
    int v1 = (int)(seed1 >>> 16);
    System.out.println("v1: " + v1);

    long seed2 = ((seed1 * multiplier + addend) & mask);
    System.out.println("seed2: " + seed2);
    int v2 = (int)(seed2 >>> 16);
    System.out.println("v2: " + v2);
}