用随机但不同的数字填充矩阵
Fill matrix with random but different numbers
我必须用一组 (-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,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,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]);
}
}
}
将 System.Random random = new System.Random();
行移到 for
循环之外。随机数生成器使用系统时间初始化(又名 "seeded"),如果您经常调用 new Random()
(如您的示例),则时间变化不够大。所以你得到相同的随机数。
将 System.Random random = new System.Random();
放在循环之外。
我必须用一组 (-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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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]);
}
}
}
将 System.Random random = new System.Random();
行移到 for
循环之外。随机数生成器使用系统时间初始化(又名 "seeded"),如果您经常调用 new Random()
(如您的示例),则时间变化不够大。所以你得到相同的随机数。
将 System.Random random = new System.Random();
放在循环之外。