c# – 尝试对一个二维数组进行随机化

huangapple go评论95阅读模式
英文:

c# - trying to randomize a 2d array

问题

  1. 我正在尝试创建一个二维数组,其中每一行的元素都是打乱顺序的。例如,在一个3x5的数组中,每一行都将包含123,但元素的顺序将不同:
  2. 我想要的结果:
  3. 1 3 2
  4. 2 1 3
  5. 2 3 1
  6. 3 1 2
  7. 1 2 3
  8. 我尝试过的代码:
  9. //初始化一个矩阵
  10. int[,] matrix = new int[3, 5];
  11. Random rnd = new Random();
  12. for (int i = 0; i < matrix.GetLength(0); i++)
  13. {
  14. for (int j = 0; j < matrix.GetLength(1); j++)
  15. {
  16. matrix[i,j] = rnd.Next(1,3);
  17. Console.WriteLine(matrix[i,j]);
  18. }
  19. Console.ReadLine();
  20. }
  21. 这似乎很简单,但作为一个新手,我为这个问题苦苦挣扎了好几天。
  22. 任何帮助或指引将不胜感激!
英文:

I am trying to make a 2d array that has shuffled elements in each row. For example, in a 3x5 array, each row will have 1, 2, and 3, but the order of the elements will be different:

what I want:
1 3 2
2 1 3
2 3 1
3 1 2
1 2 3

code I've tried:

  1. //initialize a matrix
  2. int[,] matrix = new int[3, 5];
  3. Random rnd = new Random();
  4. for (int i = 0; i &lt; matrix.GetLength(0); i++)
  5. {
  6. for (int j = 0; j &lt; matrix.GetLength(1); j++)
  7. {
  8. matrix[i,j] = rnd.Next(1,3);
  9. Console.WriteLine(matrix[i,j]);
  10. }
  11. Console.ReadLine();
  12. }

It seems fairly easy, but as a newbie, I've been struggling for days over this problem.
Any help or lead will be appreciated!

答案1

得分: 1

  1. 你可以创建一个包含123的列表,然后为每行对其进行洗牌。然后只需复制洗牌后的值:
  2. public static void Main(string[] args)
  3. {
  4. int[,] matrix = new int[5, 3];
  5. Random rnd = new Random();
  6. var values = Enumerable.Range(1, 3).ToList();
  7. for (int row = 0; row < matrix.GetLength(0); row++)
  8. {
  9. values = values.OrderBy(x => rnd.NextDouble()).ToList();
  10. for (int col = 0; col < matrix.GetLength(1); col++)
  11. {
  12. matrix[row, col] = values[col];
  13. Console.Write(matrix[row, col] + " ");
  14. }
  15. Console.WriteLine();
  16. }
  17. Console.WriteLine("按Enter键退出。");
  18. Console.ReadLine();
  19. }
  20. 示例输出:
  21. 2 3 1
  22. 3 1 2
  23. 3 2 1
  24. 1 3 2
  25. 2 1 3
  26. Enter键退出
英文:

You can make an List containing 1, 2, and 3, and then shuffle it for each row. Then just copy the shuffled values over:

  1. public static void Main(string[] args)
  2. {
  3. int[,] matrix = new int[5, 3];
  4. Random rnd = new Random();
  5. var values = Enumerable.Range(1, 3).ToList();
  6. for (int row = 0; row &lt; matrix.GetLength(0); row++)
  7. {
  8. values = values.OrderBy(x =&gt; rnd.NextDouble()).ToList();
  9. for (int col = 0; col &lt; matrix.GetLength(1); col++)
  10. {
  11. matrix[row, col] = values[col];
  12. Console.Write(matrix[row, col] + &quot; &quot;);
  13. }
  14. Console.WriteLine();
  15. }
  16. Console.WriteLine(&quot;Press Enter to Quit.&quot;);
  17. Console.ReadLine();
  18. }

Sample Output:

  1. 2 3 1
  2. 3 1 2
  3. 3 2 1
  4. 1 3 2
  5. 2 1 3
  6. Press Enter to Quit.

huangapple
  • 本文由 发表于 2023年2月16日 09:56:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75467111.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定