Skip to content

Instantly share code, notes, and snippets.

@Strelok78
Last active May 13, 2025 13:51
Show Gist options
  • Save Strelok78/0b0069ff9d23c953ccf1d3cf207b6291 to your computer and use it in GitHub Desktop.
Save Strelok78/0b0069ff9d23c953ccf1d3cf207b6291 to your computer and use it in GitHub Desktop.
Array Shuffle func
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