Askor

Hw23

Oct 13th, 2020 (edited)
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.23 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Shuffle
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int arrayLength = Convert.ToInt32(Console.ReadLine());
  10.             int[] array = new int[arrayLength];
  11.  
  12.             array = FillArray(array);
  13.  
  14.             ShowArray(array);
  15.             Shuffle(array);
  16.             Console.WriteLine();
  17.             ShowArray(array);
  18.         }
  19.  
  20.         static int[] FillArray(int[] array)
  21.         {
  22.             var random = new Random();
  23.  
  24.             for(int i = 0; i < array.Length; i++)
  25.             {
  26.                 array[i] = random.Next(10);
  27.             }
  28.  
  29.             return array;
  30.         }
  31.  
  32.         static void ShowArray(int[] array)
  33.         {
  34.             for (int i = 0; i < array.Length; i++)
  35.             {
  36.                 Console.Write($"{array[i]} ");
  37.             }
  38.         }
  39.  
  40.         static void Shuffle(int[] array)
  41.         {
  42.             var random = new Random();
  43.  
  44.             for (int i = 0; i < array.Length; i++)
  45.             {
  46.                 int key = array[i];
  47.                 int posRandom = random.Next(i, array.Length);
  48.                 array[i] = array[posRandom];
  49.                 array[posRandom] = key;
  50.             }
  51.         }
  52.     }
  53. }
  54.  
Add Comment
Please, Sign In to add comment