Last active
May 13, 2025 13:51
-
-
Save Strelok78/0b0069ff9d23c953ccf1d3cf207b6291 to your computer and use it in GitHub Desktop.
Array Shuffle func
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace iJuniorPractice | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
int[] numbers = { 1, 2, 3, 4, 5, 6 }; | |
Console.WriteLine("Origin array: "); | |
PrintArray(numbers); | |
Shuffle(numbers); | |
Console.WriteLine("Shuffled array: "); | |
PrintArray(numbers); | |
} | |
static void Shuffle<T>(T[] numbers) | |
{ | |
Random random = new Random(); | |
for (int i = numbers.Length - 1; i > 0; i--) | |
{ | |
var temp = numbers[i]; | |
int randomIndex = random.Next(numbers.Length); | |
numbers[i] = numbers[randomIndex]; | |
numbers[randomIndex] = temp; | |
} | |
} | |
static void PrintArray<T>(T[] numbers) | |
{ | |
foreach (var number in numbers) | |
{ | |
Console.Write(number); | |
} | |
Console.WriteLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment