简体   繁体   English

Java随机数不是随机的?

[英]Java random numbers not random?

I was trying to explain the random number generator in Java to a friend when he kept getting the same numbers every time he ran the program. 我试图用Java解释随机数生成器给朋友,因为他每次运行程序时都会得到相同的数字。 I created my own simpler version of the same thing and I too am getting the same exact numbers he was getting every time I run the program. 我创建了自己的同一个更简单的版本,我也得到了每次运行程序时获得的相同数字。

What am I doing wrong? 我究竟做错了什么?

import java.util.*;

public class TestCode{
   public static void main(String[] args){
       int sum = 0;
       Random rand = new Random(100);
       for(int x = 0; x < 100; x++){
           int num = (rand.nextInt(100)) + 1;
           sum += num;
           System.out.println("Random number:" + num);
       }
       //value never changes with repeated program executions.
       System.out.println("Sum: " + sum); 
   }

}

The final five numbers out of the 100 are: 100个中的最后五个数字是:

40
60
27
56
53

You have seeded the random generator with a constant value 100 . 你已经为随机生成器播种了一个常数值100 It's deterministic, so that will generate the same values each run. 它是确定性的,因此每次运行都会产生相同的值。

I'm not sure why you chose to seed it with 100 , but the seed value has nothing to do with the range of values that are generated (that's controlled by other means, such as the call to nextInt that you already have). 我不确定为什么你选择用100种子播种它,但是种子值与生成的值的范围无关(由其他方法控制,例如对你已经拥有的nextInt的调用)。

To get different values each time, use the Random constructor with no arguments, which uses the system time to seed the random generator. 要每次获取不同的值,请使用不带参数的Random构造函数,该构造函数使用系统时间为随机生成器设定种子。

Quoting from the Javadoc for the parameterless Random constructor: 从Javadoc引用无参数的Random构造函数:

Creates a new random number generator. 创建一个新的随机数生成器。 This constructor sets the seed of the random number generator to a value very likely to be distinct from any other invocation of this constructor. 此构造函数将随机数生成器的种子设置为非常可能与此构造函数的任何其他调用不同的值。

Quoting the actual code in the parameterless Random constructor: 在无参数的Random构造函数中引用实际代码:

public Random() {
    this(seedUniquifier() ^ System.nanoTime());
}

This: 这个:

   Random rand = new Random(100);

You're giving the random number generator the same seed (100) each time you start the program. 每次启动程序时,您都会为随机数生成器提供相同的种子(100)。 Give it something like the output from System.currentTimeMillis() and that should give you different numbers for each invocation. 给它类似System.currentTimeMillis()的输出,它应该为每个调用提供不同的数字。

Random number generators are really only pseudo-random. 随机数生成器实际上只是伪随机数。 That is, they use deterministic means to generate sequences that appear random given certain statistical criteria. 也就是说,他们使用确定性手段来生成在给定某些统计标准下随机出现的序列。

The Random(long seed) constuctor allows you to pass in a seed that determines the sequence of pseudo-random numbers. Random(long seed)构造器允许您传入确定伪随机数序列的种子。

Hope this helps.. 希望这可以帮助..

Random r = new Random(System.currentTimeMillis());
double[] rand = new double[500];
for(int i=0;i<100;i++){
    rand[i] = r.nextDouble();
    //System.out.print(rand[i] + "\n");
}
//System.out.println();
return rand[randomInt];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM