【发布时间】: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,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,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]);
}
}
}
【问题讨论】: