Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.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
C# 如何使用循环在两个数字之间的矩阵中设置随机数_C#_Arrays_Loops - Fatal编程技术网

C# 如何使用循环在两个数字之间的矩阵中设置随机数

C# 如何使用循环在两个数字之间的矩阵中设置随机数,c#,arrays,loops,C#,Arrays,Loops,我想随机生成两个矩阵数组,这样我以后就可以把它们相加并存储到第三个矩阵中,我该怎么做呢?几乎完全失去了,以下是我目前所拥有的 using System; namespace question2_addingrandommatrice { class Program { static void Main(string[] args) { Random random = new Random();

我想随机生成两个矩阵数组,这样我以后就可以把它们相加并存储到第三个矩阵中,我该怎么做呢?几乎完全失去了,以下是我目前所拥有的

    using System;

namespace question2_addingrandommatrice
{
    class Program
    {
        static void Main(string[] args)
        {
            Random random = new Random();

            int[,] newarray = new int[3, 3];

            for (int i = 0; i < 3; i++)
            {
                

                for (int j = 0; j < 3; j++)
                {
                    int ran2 = random.Next(-10, 10);
                    int ran1 = random.Next(-10, 10);
                    newarray[i, j] = ran1, ran2;

                }
            }
            

            Console.ReadKey();
        }
    }
}

你就快到了,你只需要一个随机的。下一个

这里有一个方法可以帮你做到这一点

private static int[,] GenerateRandomMatrix(int x, int y)
{
   var array = new int[x, y];

   for (int i = 0; i < array.GetLength(0); i++)
      for (int j = 0; j < array.GetLength(1); j++)
         array[i, j] = random.Next(-10, 10);
   return array;
}
额外资源


你可以简单地做到这一点

Random random = new Random();

int[,] newarray = new int[3, 3];

for (int i = 0; i < 3; i++)
{
    for (int j = 0; j < 3; j++)
        newarray[i, j] = random.Next(-10, 10); ;
}

哦,天哪,谢谢。我一直在为此扯头发。
Random random = new Random();

int[,] newarray = new int[3, 3];

for (int i = 0; i < 3; i++)
{
    for (int j = 0; j < 3; j++)
        newarray[i, j] = random.Next(-10, 10); ;
}