Advertisement
TargeTPoweR

Shuffle Method

Mar 28th, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Tasks
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. int[] array1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  14. Shuffle(array1);
  15.  
  16. Console.WriteLine(" ");
  17. Console.WriteLine("Матрица после перетасовки.");
  18.  
  19. for (int i = 0; i < array1.Length; i++)
  20. {
  21. Console.Write(array1[i] + " ");
  22. }
  23. }
  24. static void Shuffle(int[] array)
  25. {
  26. Random rand = new Random();
  27.  
  28. for (int i = array.Length - 1; i >= 0; i--)
  29. {
  30. int elementIndexForChange = rand.Next(0, i + 1);
  31. int temporaryPlace = array[elementIndexForChange];
  32. array[elementIndexForChange] = array[i];
  33. array[i] = temporaryPlace;
  34. }
  35. }
  36. }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement