holllowknight

ДЗ: Shuffle

Mar 17th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.95 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 Shuffle
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Random rand = new Random();
  14.             int[] array = new int[9] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  15.             Shuffle(array, rand);
  16.             for(int i = 0; i < array.Length; i++)
  17.             {
  18.                 Console.Write($"{array[i]} ");
  19.             }
  20.             Console.WriteLine();
  21.         }
  22.  
  23.         static void Shuffle(int[] array, Random rand)
  24.         {
  25.             int exchVar;
  26.             int replaceIndex;
  27.             for(int i = 0; i < array.Length - 1; i++)
  28.             {
  29.                 replaceIndex = rand.Next(i + 1, array.Length);
  30.                 exchVar = array[replaceIndex];
  31.                 array[replaceIndex] = array[i];
  32.                 array[i] = exchVar;
  33.             }
  34.         }
  35.     }
  36. }
Add Comment
Please, Sign In to add comment