Math.random() versus Random.nextInt(int)

后端 未结 4 1392
灰色年华
灰色年华 2020-11-22 07:21

What is the difference between Math.random() * n and Random.nextInt(n) where n is an integer?

相关标签:
4条回答
  • 2020-11-22 07:58

    Here is the detailed explanation of why "Random.nextInt(n) is both more efficient and less biased than Math.random() * n" from the Sun forums post that Gili linked to:

    Math.random() uses Random.nextDouble() internally.

    Random.nextDouble() uses Random.next() twice to generate a double that has approximately uniformly distributed bits in its mantissa, so it is uniformly distributed in the range 0 to 1-(2^-53).

    Random.nextInt(n) uses Random.next() less than twice on average- it uses it once, and if the value obtained is above the highest multiple of n below MAX_INT it tries again, otherwise is returns the value modulo n (this prevents the values above the highest multiple of n below MAX_INT skewing the distribution), so returning a value which is uniformly distributed in the range 0 to n-1.

    Prior to scaling by 6, the output of Math.random() is one of 2^53 possible values drawn from a uniform distribution.

    Scaling by 6 doesn't alter the number of possible values, and casting to an int then forces these values into one of six 'buckets' (0, 1, 2, 3, 4, 5), each bucket corresponding to ranges encompassing either 1501199875790165 or 1501199875790166 of the possible values (as 6 is not a disvisor of 2^53). This means that for a sufficient number of dice rolls (or a die with a sufficiently large number of sides), the die will show itself to be biased towards the larger buckets.

    You will be waiting a very long time rolling dice for this effect to show up.

    Math.random() also requires about twice the processing and is subject to synchronization.

    0 讨论(0)
  • 2020-11-22 08:03

    According to https://forums.oracle.com/forums/thread.jspa?messageID=6594485&#6594485 Random.nextInt(n) is both more efficient and less biased than Math.random() * n

    0 讨论(0)
  • 2020-11-22 08:13

    According to this example Random.nextInt(n) has less predictable output then Math.random() * n. According to [sorted array faster than an unsorted array][1] I think we can say Random.nextInt(n) is hard to predict.

    usingRandomClass : time:328 milesecond.

    usingMathsRandom : time:187 milesecond.

    package javaFuction;
    import java.util.Random;
    public class RandomFuction 
    {
        static int array[] = new int[9999];
        static long sum = 0;
        public static void usingMathsRandom() {
            for (int i = 0; i < 9999; i++) {
             array[i] = (int) (Math.random() * 256);
           }
    
            for (int i = 0; i < 9999; i++) {
                for (int j = 0; j < 9999; j++) {
                    if (array[j] >= 128) {
                        sum += array[j];
                    }
                }
            }
        }
    
        public static void usingRandomClass() {
            Random random = new Random();
            for (int i = 0; i < 9999; i++) {
                array[i] = random.nextInt(256);
            }
    
            for (int i = 0; i < 9999; i++) {
                for (int j = 0; j < 9999; j++) {
                    if (array[j] >= 128) {
                        sum += array[j];
                    }
                }
    
            }
    
        }
    
        public static void main(String[] args) {
            long start = System.currentTimeMillis();
            usingRandomClass();
            long end = System.currentTimeMillis();
            System.out.println("usingRandomClass " + (end - start));
            start = System.currentTimeMillis();
            usingMathsRandom();
            end = System.currentTimeMillis();
            System.out.println("usingMathsRandom " + (end - start));
    
        }
    
    }
    
    0 讨论(0)
  • 2020-11-22 08:19

    another important point is that Random.nextInt(n) is repeatable since you can create two Random object with the same seed. This is not possible with Math.random().

    0 讨论(0)
提交回复
热议问题