Advertisement
TwinFrame

ShuffleMassive

Jan 15th, 2020
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Clight_17_Shuffle
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. bool openMenu = true;
  10. int[,] massive = { { 1, 2, 3 }, { 1, 2, 1 }, { 1, 1, 1 } };
  11.  
  12. while (openMenu == true)
  13. {
  14. Print(massive);
  15. Shuffle(massive);
  16. Console.ReadKey();
  17. Console.Clear();
  18. }
  19. }
  20. static void Shuffle(int[,] massive)
  21. {
  22. int[,] currentMassive = massive;
  23. int currentElement = 0;
  24. int randomI;
  25. int randomJ;
  26. Random random = new Random();
  27. for (int i = 0; i < currentMassive.GetLength(0); i++)
  28. {
  29. for (int j = 0; j < currentMassive.GetLength(1); j++)
  30. {
  31. currentElement = currentMassive[i, j];
  32. randomI = random.Next(0, currentMassive.GetLength(0));
  33. randomJ = random.Next(0, currentMassive.GetLength(1));
  34. currentMassive[i, j] = currentMassive[randomI, randomJ];
  35. currentMassive[randomI, randomJ] = currentElement;
  36. }
  37. Console.WriteLine();
  38. }
  39. massive = currentMassive;
  40. }
  41. static void Print(int[,] massive)
  42. {
  43. for (int i = 0; i < massive.GetLength(0); i++)
  44. {
  45. for (int j = 0; j < massive.GetLength(1); j++)
  46. {
  47. Console.Write($"{massive[i, j]} ");
  48. }
  49. Console.WriteLine();
  50. }
  51. }
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement