Advertisement
MaoChessy

Task 23 - fix

Oct 31st, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2.  
  3. namespace C_sharp_Light
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int[] array = new int[10];
  10.             Console.WriteLine("Массив до перемешивания");
  11.             for (int i = 0; i < array.Length; i++)
  12.             {
  13.                 array[i] = i;
  14.                 Console.Write(array[i]+" ");
  15.             }
  16.             Console.WriteLine();
  17.  
  18.             Shuffle(array);
  19.             Console.WriteLine("Массив после перемешивания");
  20.             for (int i = 0; i < array.Length; i++)
  21.             {
  22.                 Console.Write(array[i] + " ");
  23.             }
  24.             Console.ReadKey();
  25.         }
  26.        
  27.         static void Shuffle(int[] array)
  28.         {
  29.             Random rand = new Random();
  30.             int temp, posOne, posTwo;
  31.             for (int i = 0; i < array.Length*2; i++)
  32.             {
  33.                 posOne = rand.Next(0, array.Length);
  34.                 posTwo = rand.Next(0, array.Length);
  35.                 while (posOne == posTwo)
  36.                 {
  37.                     posTwo = rand.Next(0, array.Length);
  38.                 }
  39.                 temp = array[posOne];
  40.                 array[posOne] = array[posTwo];
  41.                 array[posTwo] = temp;
  42.             }
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement