Java 随机生成器的种子产生不同的输出
Java Random Generator's seed producing different outputs
在尝试使用创建时传递给对象的两个特定种子来创建 Coin 对象 class 时,我注意到在将种子传递给 int "seed" 时,种子变量产生与仅将特定数字输入随机数生成器不同的变量。这是来自 Coin class:
的一些代码
public int headCount;
public int tailCount;
public int seed;
public Coin( int n ){
seed = n;
headCount = 0;
tailCount = 0;
}
public Random flipGenerator = new Random(seed);
public String flip(){
String heads = "H";
String tails = "T";
boolean nextFlip = flipGenerator.nextBoolean();
if (nextFlip == true)
{
headCount++;
return heads;
}
if (nextFlip == false)
{
tailCount++;
return tails;
}
return null;
}
下面是创建和打印 Coin 对象的文件:
Coin coin1 = new Coin( 17 );
Coin coin2 = new Coin( 13 );
该文件中的代码使用 17 号种子打印出随机翻转 20 次的结果,使用 13 号种子打印 10 次,最后再次使用 17 号种子打印 35 次。但是,使用
时输出不正确
public Random flipGenerator = new Random(seed);
相对于
public Random flipGenerator = new Random(17);
或
public Random flipGenerator = new Random(13);
为什么会这样?
在调用构造函数之前初始化实例字段。
按照执行顺序,这段代码:
public int headCount;
public int tailCount;
public int seed;
public Coin( int n ){
seed = n;
headCount = 0;
tailCount = 0;
}
public Random flipGenerator = new Random(seed);
在功能上等同于:
public int headCount;
public int tailCount;
public int seed;
public Random flipGenerator = new Random(seed);
public Coin( int n ){
seed = n;
headCount = 0;
tailCount = 0;
}
在 Java 中,任何未显式初始化的字段都被赋予值 zero/null/false,因此您 总是 做 flipGenerator = new Random(0)
.当您在构造函数中初始化 seed
时,已经创建了 Random 对象。实例字段在构造函数之后声明的事实无关紧要,因为所有实例字段及其初始化表达式都在调用构造函数之前执行。
每当您初始化 Coin
class 时,它都会使用 0 作为种子。不管你是否将 flipGenerator
初始化放在构造函数下面,它仍然会在构造函数中被调用,因为它是一个实例变量。
Coin coin1 = new Coin( 17 );
Coin coin2 = new Coin( 13 );
此代码使用 0,因为在您传递种子时,flipGenerator 已使用默认种子 0 进行初始化。
你需要这样做
public int headCount;
public int tailCount;
public int seed;
public Random flipGenerator;
public Coin( int n ){
seed = n;
headCount = 0;
tailCount = 0;
flipGenerator = new Random(seed);
}
...
}
在尝试使用创建时传递给对象的两个特定种子来创建 Coin 对象 class 时,我注意到在将种子传递给 int "seed" 时,种子变量产生与仅将特定数字输入随机数生成器不同的变量。这是来自 Coin class:
的一些代码public int headCount;
public int tailCount;
public int seed;
public Coin( int n ){
seed = n;
headCount = 0;
tailCount = 0;
}
public Random flipGenerator = new Random(seed);
public String flip(){
String heads = "H";
String tails = "T";
boolean nextFlip = flipGenerator.nextBoolean();
if (nextFlip == true)
{
headCount++;
return heads;
}
if (nextFlip == false)
{
tailCount++;
return tails;
}
return null;
}
下面是创建和打印 Coin 对象的文件:
Coin coin1 = new Coin( 17 );
Coin coin2 = new Coin( 13 );
该文件中的代码使用 17 号种子打印出随机翻转 20 次的结果,使用 13 号种子打印 10 次,最后再次使用 17 号种子打印 35 次。但是,使用
时输出不正确public Random flipGenerator = new Random(seed);
相对于
public Random flipGenerator = new Random(17);
或
public Random flipGenerator = new Random(13);
为什么会这样?
在调用构造函数之前初始化实例字段。
按照执行顺序,这段代码:
public int headCount;
public int tailCount;
public int seed;
public Coin( int n ){
seed = n;
headCount = 0;
tailCount = 0;
}
public Random flipGenerator = new Random(seed);
在功能上等同于:
public int headCount;
public int tailCount;
public int seed;
public Random flipGenerator = new Random(seed);
public Coin( int n ){
seed = n;
headCount = 0;
tailCount = 0;
}
在 Java 中,任何未显式初始化的字段都被赋予值 zero/null/false,因此您 总是 做 flipGenerator = new Random(0)
.当您在构造函数中初始化 seed
时,已经创建了 Random 对象。实例字段在构造函数之后声明的事实无关紧要,因为所有实例字段及其初始化表达式都在调用构造函数之前执行。
每当您初始化 Coin
class 时,它都会使用 0 作为种子。不管你是否将 flipGenerator
初始化放在构造函数下面,它仍然会在构造函数中被调用,因为它是一个实例变量。
Coin coin1 = new Coin( 17 );
Coin coin2 = new Coin( 13 );
此代码使用 0,因为在您传递种子时,flipGenerator 已使用默认种子 0 进行初始化。
你需要这样做
public int headCount;
public int tailCount;
public int seed;
public Random flipGenerator;
public Coin( int n ){
seed = n;
headCount = 0;
tailCount = 0;
flipGenerator = new Random(seed);
}
...
}