Avatar ·

How to randomly shuffle a List<T> in C#?

Hello everyone! I'm a beginner C# developer and I have a problem. I need to randomly shuffle the elements in a `List`. I'm writing a card game application and I need to shuffle a deck of cards. I have a `List deck` that I fill with cards. I tried using `Random`, but I can't get the elements to shuffle correctly. Here's my current code: ```csharp List numbers = new List { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; Random rnd = new Random(); // My attempt to shuffle for (int i = 0; i < numbers.Count; i++) { int randomIndex = rnd.Next(numbers.Count); numbers[i] = numbers[randomIndex]; // This just copies the value, doesn't swap } ``` I understand this code is wrong — I'm losing element values instead