math . random()和Random.nextInt(int)

时间:2022-11-11 13:31:55

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

Math.random() * n和Random.nextInt(n)中n是整数的区别是什么?

4 个解决方案

#1


142  

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:

以下是关于为什么“随机性。nextint (n)”比“数学。random() * n”更有效、更不偏颇的详细解释。

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

内部math . random()使用Random.nextDouble()。

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.nextDouble()使用Random.next()两次生成一个约有两倍均匀分布位尾数,所以它是均匀分布在0到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.

Random.nextInt(n)使用Random.next()的时间平均不到两次——它使用一次,如果n的值高于最高获得多个低于MAX_INT再次尝试,否则是返回值模n(这可以防止上面的值最高的n下面MAX_INT倾斜分布)的倍数,那么返回一个值均匀分布在0到n - 1范围。

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

比例由6之前,math . random()的输出是2 ^ 53个可能值来自均匀分布。

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.

比例由6不改变可能值的数量,和铸造int然后部队这些值的六个“桶”(0,1,2,3,4,5),每个bucket对应范围包括1501199875790165或1501199875790166的可能值(2 ^ 6不是disvisor 53)。这意味着,对于足够数量的掷骰(或者骰子有足够多的边),骰子会显示自己偏向于较大的掷骰。

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.

Math.random()也需要大约两倍的处理,需要进行同步。

#2


26  

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().

另一个要点是Random.nextInt(n)是可重复的,因为您可以使用相同的种子创建两个随机对象。使用Math.random()是不可能的。

#3


14  

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

据https://forums.oracle.com/forums/thread.jspa?messageID=6594485� Random.nextInt(n)比Math.random() * n更有效,更不带有偏见

#4


0  

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.

根据这个例子,Random.nextInt(n)的可预测输出比Math.random() * n少。

usingRandomClass : time:328 milesecond.

milesecond usingRandomClass:时间:328。

usingMathsRandom : time:187 milesecond.

milesecond usingMathsRandom:时间:187。

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));

    }

}

#1


142  

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:

以下是关于为什么“随机性。nextint (n)”比“数学。random() * n”更有效、更不偏颇的详细解释。

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

内部math . random()使用Random.nextDouble()。

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.nextDouble()使用Random.next()两次生成一个约有两倍均匀分布位尾数,所以它是均匀分布在0到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.

Random.nextInt(n)使用Random.next()的时间平均不到两次——它使用一次,如果n的值高于最高获得多个低于MAX_INT再次尝试,否则是返回值模n(这可以防止上面的值最高的n下面MAX_INT倾斜分布)的倍数,那么返回一个值均匀分布在0到n - 1范围。

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

比例由6之前,math . random()的输出是2 ^ 53个可能值来自均匀分布。

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.

比例由6不改变可能值的数量,和铸造int然后部队这些值的六个“桶”(0,1,2,3,4,5),每个bucket对应范围包括1501199875790165或1501199875790166的可能值(2 ^ 6不是disvisor 53)。这意味着,对于足够数量的掷骰(或者骰子有足够多的边),骰子会显示自己偏向于较大的掷骰。

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.

Math.random()也需要大约两倍的处理,需要进行同步。

#2


26  

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().

另一个要点是Random.nextInt(n)是可重复的,因为您可以使用相同的种子创建两个随机对象。使用Math.random()是不可能的。

#3


14  

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

据https://forums.oracle.com/forums/thread.jspa?messageID=6594485� Random.nextInt(n)比Math.random() * n更有效,更不带有偏见

#4


0  

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.

根据这个例子,Random.nextInt(n)的可预测输出比Math.random() * n少。

usingRandomClass : time:328 milesecond.

milesecond usingRandomClass:时间:328。

usingMathsRandom : time:187 milesecond.

milesecond usingMathsRandom:时间:187。

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));

    }

}