【问题标题】:Fill matrix with random but different numbers [duplicate]用随机但不同的数字填充矩阵[重复]
【发布时间】:2015-09-30 19:51:57
【问题描述】:

我必须用一组 (-1,0,1) 中的随机数填充矩阵... 我的代码是这样做的,但是我希望我的矩阵里面有更多不同的元素。 我试着用一个例子更好地解释自己。

我的代码的输出之一是:

-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1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,0,0,-1,-1,0,-1,0,0,0,1,0,1,0,-1,1,0,0,0,0,0,1
0,1,0,0,0,1,-1,1,-1,0,0,0,1,1,-1,1,1,1,0,0,0,0,0,1,1,1,-1,1,1,1,-1
0,-1,0,0,0,0,0,-1,0,0,0,1,-1,1,0,-1,0,0,0,0,0,0,0,-1,1,1,0,0,0,0,-1
0,-1,0,0,0,1,1,1,-1,0,0,1,1,1,0,-1,0,0,0,0,0,0,0,0,1,1,-1,0,0,0,-1
1,-1,1,0,1,0,1,1,0,0,0,1,1,0,-1,-1,0,0,1,0,0,0,0,1,1,1,0,1,-1,0,-1
0,-1,0,0,0,-1,-1,-1,1,1,1,0,-1,-1,-1,-1,1,1,0,1,0,1,0,-1,1,1,-1,0,-1,1,1
0,-1,0,0,0,-1,0,0,1,1,0,1,-1,-1,0,-1,1,1,0,1,0,1,0,1,1,-1,-1,0,0,1,1
0,-1,0,1,0,-1,0,-1,0,0,0,0,1,-1,1,-1,1,1,0,0,0,0,0,-1,0,1,-1,0,1,1,1
0,1,0,0,0,0,0,1,-1,1,0,0,1,0,-1,1,0,0,0,1,0,1,0,-1,0,1,0,0,0,0,-1
1,1,1,0,1,1,-1,1,-1,0,0,1,1,0,0,1,1,1,0,0,0,0,0,1,0,1,-1,0,1,1,-1
0,-1,0,0,0,-1,-1,-1,0,0,0,1,-1,0,-1,-1,0,0,0,0,0,0,0,-1,1,1,-1,0,0,0,1
0,-1,0,0,0,-1,-1,1,0,0,0,0,1,-1,-1,-1,0,0,0,0,0,0,0,-1,1,0,-1,0,0,0,1
0,-1,0,0,0,0,1,1,-1,0,0,0,1,0,1,-1,1,1,0,0,0,0,0,1,0,1,-1,1,-1,1,-1
0,-1,0,0,0,-1,1,1,0,0,0,1,1,0,-1,-1,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,1

所以我的输出中没有很多相同的行。

但我不知道该怎么做……有可能吗?

我的代码:

static void Main()
{
    int righe = 2432; //rows
    int colonne=31; //coloumns

    int[,] matrice = new int[righe, colonne]; 

    for(int i = 0; i<righe; i++) 
    {
        for (int y = 0; y < colonne; y++)
        {
            System.Random random = new System.Random();

            int number = random.Next(0, 2); 
            int[] choices = new int[3] { -1, 0, 1 }; 
            matrice[i, y] = choices[number]; //insert the random value
            Console.WriteLine(matrice[i, y]); 
        }
    } 
}

【问题讨论】:

    标签: c# matrix random numbers


    【解决方案1】:

    System.Random random = new System.Random(); 行移到for 循环之外。随机数生成器使用系统时间初始化(又名“种子”),如果您经常调用new Random()(如您的示例中),则时间变化不够。所以你会得到相同的随机数。

    【讨论】:

      【解决方案2】:

      System.Random random = new System.Random(); 放在循环之外。

      【讨论】:

      • 通常更愿意解释正在发生的事情,而不是仅仅给出建议,因为我们试图教人们一些东西,而不是仅仅解决一个问题,然后让它一次又一次地蔓延,因为他们没有不明白根本问题。有关示例,请参见 the other answer
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-27
      • 2021-11-04
      • 1970-01-01
      • 2020-05-18
      • 2012-03-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多