kisame1313

1.3.9.6

Jul 19th, 2017
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.04 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ConsoleApplication
  4. {
  5.     class Program
  6.     {
  7.         /*
  8.          * Реализуйте метод Shuffle, который перемешивает элементы массива в случайном порядке.
  9.          */
  10.  
  11.         static void Main ( string [] args )
  12.         {
  13.             int [] nums = new int [10] {1,2,3,4,5,6,7,8,9,0 };
  14.             nums = Shuffle ( nums );
  15.             foreach ( int num in nums )
  16.             {
  17.                 Console.WriteLine (num);
  18.             }
  19.         }
  20.         static int [] Shuffle ( int [] originalArray )
  21.         {
  22.             int [] newArray = new int [originalArray.Length];
  23.             string usedIndexes = string.Empty;
  24.             int temp;
  25.             int randomIndex;
  26.             Random rand = new Random ();
  27.  
  28.             for ( int i = 0 ; i < newArray.Length ; i++ )
  29.             {
  30.                 temp = originalArray [i];
  31.                 randomIndex = rand.Next ( 0, newArray.Length );
  32.  
  33.                 while (usedIndexes.Contains(Convert.ToString( randomIndex)) )
  34.                 {
  35.                     randomIndex = rand.Next ( 0, newArray.Length );
  36.                 }
  37.                 newArray [randomIndex] = originalArray [i];
  38.                 usedIndexes += " " + randomIndex;
  39.             }
  40.             return newArray;
  41.         }
  42.     }
  43. }
Add Comment
Please, Sign In to add comment