Advertisement
nikitaTheSlayer

Lesson: shuffle

Apr 20th, 2020
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.71 KB | None | 0 0
  1. using System;
  2.  
  3. namespace CSLight_4._3
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8 };
  10.  
  11.             Shuffle(arr);
  12.  
  13.             for (int i = 0; i < arr.Length; i++)
  14.             {
  15.                 Console.Write(arr[i]);
  16.             }
  17.         }
  18.  
  19.         public static void Shuffle(int[] arr)
  20.         {
  21.             Random rand = new Random();
  22.  
  23.             for (int i = 0; i < arr.Length; i++)
  24.             {
  25.                 int randIndex = rand.Next(i + 1);
  26.  
  27.                 int tempVariable = arr[randIndex];
  28.                 arr[randIndex] = arr[i];
  29.                 arr[i] = tempVariable;
  30.             }
  31.         }
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement