Anonim_999

Shuffler

Mar 8th, 2021 (edited)
970
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.86 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Shuffler
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             string[] words = {"apple","orange","tomato","island","carrot"};
  10.             Shuffle(words);
  11.  
  12.             foreach (string word in words)
  13.             {
  14.                 Console.Write($"{word} ");
  15.             }
  16.  
  17.             Console.ReadKey();
  18.         }
  19.  
  20.         private static void Shuffle(string[] array)
  21.         {
  22.             Random rand = new Random();
  23.  
  24.             for (int i = 0; i < array.Length; i++)
  25.             {
  26.                 int randomIndex = rand.Next(0,array.Length);
  27.                 int randomIndex2 = rand.Next(0,array.Length);
  28.                 string temp = array[randomIndex];
  29.                 array[randomIndex] = array[randomIndex2];
  30.                 array[randomIndex2] = temp;
  31.             }
  32.         }
  33.     }
  34. }
  35.  
Add Comment
Please, Sign In to add comment