Advertisement
holllowknight

ДЗ: Shuffle

Mar 18th, 2020 (edited)
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.00 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[] numbers = new int[9];
  10.  
  11.             for (int i = 0; i < numbers.Length; i++)
  12.                 numbers[i] = i;
  13.  
  14.             PrintArray(numbers);
  15.             Shuffle(numbers);
  16.             PrintArray(numbers);
  17.         }
  18.  
  19.         static void Shuffle(int[] array)
  20.         {
  21.             Random random = new Random();
  22.             int tempValue;
  23.             int tempIndex;
  24.  
  25.             for (int i = 0; i < array.Length - 1; i++)
  26.             {
  27.                 tempIndex = random.Next(i + 1, array.Length);
  28.                 tempValue = array[tempIndex];
  29.                 array[tempIndex] = array[i];
  30.                 array[i] = tempValue;
  31.             }
  32.         }
  33.  
  34.         static void PrintArray(int[] array)
  35.         {
  36.             for (int i = 0; i < array.Length; i++)
  37.                 Console.Write($"{array[i]} ");
  38.  
  39.             Console.WriteLine();
  40.         }
  41.     }
  42. }
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement