What's with 181783497276652981 and 8682522807148012 in Random (Java 7)?

后端 未结 3 1173
天涯浪人
天涯浪人 2021-01-29 21:25

Why were 181783497276652981 and 8682522807148012 chosen in Random.java?

Here\'s the relevant source code from Java SE JDK 1.7:

3条回答
  •  醉梦人生
    2021-01-29 21:53

    If you consider that the equation used for the random number generator is:

    LCGEquation

    Where X(n+1) is the next number, a is the multipler, X(n) is the current number, c is the increment and m is the modulus.

    If you look further into Random, a, c and m are defined in the header of the class

    private static final long multiplier = 0x5DEECE66DL;   //= 25214903917 -- 'a'
    private static final long addend = 0xBL;               //= 11          -- 'c'
    private static final long mask = (1L << 48) - 1;       //= 2 ^ 48 - 1  -- 'm'
    

    and looking at the method protected int next(int bits) this is were the equation is implemented

    nextseed = (oldseed * multiplier + addend) & mask;
    //X(n+1) =  (X(n)   *      a     +    c  ) mod m
    

    This implies that the method seedUniquifier() is actually getting X(n) or in the first case at initialisation X(0) which is actually 8682522807148012 * 181783497276652981, this value is then modified further by the value of System.nanoTime(). This algorithm is consistent with the equation above but with the following X(0) = 8682522807148012, a = 181783497276652981, m = 2 ^ 64 and c = 0. But as the mod m of is preformed by the long overflow the above equation just becomes

    eq2

    Looking at the paper, the value of a = 1181783497276652981 is for m = 2 ^ 64, c = 0. So it appears to just be a typo and the value 8682522807148012 for X(0) which appears to be a seeming randomly chosen number from legacy code for Random. As seen here. But the merit of these chosen numbers could still be valid but as mentioned by Thomas B. probably not as "good" as the one in the paper.

    EDIT - Below original thoughts have since been clarified so can be disregarded but leaving it for reference

    This leads me the conclusions:

    1. The reference to the paper is not for the value itself but for the methods used to obtain the values due to the different values of a, c and m

    2. It is mere coincidence that the value is otherwise the same other than the leading 1 and the comment is misplaced (still struggling to believe this though)

    OR

    There has been a serious misunderstanding of the tables in the paper and the developers have just chosen a value at random as by the time it is multiplied out what was the point in using the table value in the first place especially as you can just provide your own seed value any way in which case these values are not even taken into account

    So to answer your question

    Could other numbers have been chosen that would have worked as well as these two numbers? Why or why not?

    Yes, any number could have been used, in fact if you specify a seed value when you Instantiate Random you are using any other value. This value does not have any effect on the performance of the generator, this is determined by the values of a,c and m which are hard coded within the class.

提交回复
热议问题