MayloGreen

DZ_functions_ShuffleFunction

Dec 12th, 2021 (edited)
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.34 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace DZ_functions_ShuffleFunction
  5. {
  6.     class Program
  7.     {
  8.         public static void Main(string[] args)
  9.         {
  10.             int[] array = Enumerable.Range(1, 10).ToArray();
  11.             OutputArray(array);
  12.             ShuffleArray(array);
  13.  
  14.             Console.Write("\nПеремешанный порядок чисел в массиве: ");
  15.  
  16.             for (int i = 0; i < array.Length; i++)
  17.                 Console.Write(array[i] + " ");
  18.  
  19.             Console.Write("\n\nНажмите любую клавишу для завершения программы...");
  20.             Console.ReadKey();
  21.  
  22.             Console.WriteLine();
  23.         }
  24.    
  25.         public static void ShuffleArray( int[] array)
  26.         {
  27.             Random random = new Random();
  28.  
  29.             for (int i = 0; i < array.Length; i++)
  30.             {
  31.                 int randomIndex = random.Next(array.Length - 1);
  32.  
  33.                 int temp = array[i];
  34.                 array[i] = array[randomIndex];
  35.                 array[randomIndex] = temp;
  36.             }
  37.         }
  38.  
  39.         static void OutputArray(int[] array)
  40.         {
  41.             Console.Write("Текущий порядок чисел в массиве: ");
  42.  
  43.             foreach (var item in array)
  44.                 Console.Write(item + " ");
  45.         }
  46.     }
  47. }
Add Comment
Please, Sign In to add comment