Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java random在两个位置具有不同的行为_Java_Random - Fatal编程技术网

Java random在两个位置具有不同的行为

Java random在两个位置具有不同的行为,java,random,Java,Random,我已经厌倦了在谷歌上搜索而没有找到解决方案,所以 以下是相关代码: public class Main { public char source[] = { 'd', 'o', 'i', 't', 'r', 'e', 'c', 'n', 'x', 'y' }; //... // I don't have a given seed, which is the right approach, // from what I've read until now Rando

我已经厌倦了在谷歌上搜索而没有找到解决方案,所以

以下是相关代码:

public class Main {

  public char source[] = { 'd', 'o', 'i', 't', 'r', 'e', 'c', 'n', 'x', 'y' }; 

  //...
  // I don't have a given seed, which is the right approach, 
  //   from what I've read until now
  Random rand = new Random(); 
  //...
  public void init() {
      char config[] = new char[10];

      int life= 0;
      int pos;
      for (int j = 0; j < 100; j++) {
          for (int i = 0; i < 10; i++) {
              do {
                  pos = rand.nextInt(10);
              } while (source[pos] == '?');
              config[i] = source[pos];
              source[pos] = '?';
          }
          life= rand.nextInt((30 + 1) -1);
          population[j] = new Individual(config, 0, life);
          //...
      }
  }
  //...
}
main():


问题不在于数字生成器,而是传递给所有个人的
config
数组。数组是引用对象,因此当您在循环中更改
config
以准备下一个个体时,这些更改将在您之前创建的
individual
的所有实例中可见

您需要在
单个
的构造函数中复制一份
配置
,或者在
循环的每次迭代中使用一个新数组:

int life= 0;
int pos;
for (int j = 0; j < 100; j++) {
    char config[] = new char[10]; // <<== Move the declaration here
    for (int i = 0; i < 10; i++) {
        do {
            pos = rand.nextInt(10);
        } while (source[pos] == '?');
        config[i] = source[pos];
        source[pos] = '?';
    }
    life= rand.nextInt((30 + 1) -1);
    population[j] = new Individual(config, 0, life);
}
int-life=0;
int pos;
对于(int j=0;j<100;j++){

char config[]=new char[10];//不……Java应用程序在哪里声明和初始化了
source
数组?
dasblinkenlight
很好。你已经得到了答案。哦……这就是问题所在……我只关注
Random
…谢谢!
public static void main(String[] args) {
  Main main = new Main();
      main.init();
      for (int i = 0; i < 100; i++) {
      for (int j = 0; j < 10; j++){
          System.out.print(main.population[i].getConfig()[j] + " ");
      }
      System.out.println(main.population[i].getAge() + " " + main.populatie[i].getLife());
      }
}
...
e o x c i r y t d n 0 15
e o x c i r y t d n 0 25
e o x c i r y t d n 0 12
e o x c i r y t d n 0 22
e o x c i r y t d n 0 15
...
int life= 0;
int pos;
for (int j = 0; j < 100; j++) {
    char config[] = new char[10]; // <<== Move the declaration here
    for (int i = 0; i < 10; i++) {
        do {
            pos = rand.nextInt(10);
        } while (source[pos] == '?');
        config[i] = source[pos];
        source[pos] = '?';
    }
    life= rand.nextInt((30 + 1) -1);
    population[j] = new Individual(config, 0, life);
}