using System.Collections.Generic; public class Deck { private Listcards; public Deck() { cards = new List (); string[] suits = { "Spades", "Hearts", "Diamonds", "Clubs" }; string[] ranks = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" }; foreach (string suit in suits) { foreach (string rank in ranks) { cards.Add(rank + " of " + suit); } } } }
public void Shuffle() { Random rand = new Random(); int n = cards.Count; while (n > 1) { n--; int k = rand.Next(n + 1); string temp = cards[k]; cards[k] = cards[n]; cards[n] = temp; } }In this example, the Shuffle method uses the Fisher-Yates algorithm to randomly shuffle the cards in the deck. A new instance of the Random class is created to generate a random index between 0 and the number of remaining cards. The method then swaps the card at the current index with the one at the randomized index, until every card has been shuffled. Package library: This is a built-in library in C# and doesn't require any external packages or libraries to be installed.