I have string list of a deck of card the strings are as such, A-DIAMONDS, 2-CLUBS, etc.
I want to be able to generate 5 unique items from this list randomly.
I know how to do this in python with Random.sample(5) but in trying to find a solution in C#. everything seems to be generating a random, put it in a list, generate another random, check it against the list and it is working fine.
Is there a more compact way of doing this in C#?
Here is my full code after using Linq for shuffling.
class Program
{
static void Main(string[] args)
{
string [] cardValues = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
string [] cardSuites = { "HEARTS", "CLUBS", "DIAMONDS", "SPADES" };
List<string> deckOfCards = new List<string>();
foreach(string cardsuit in cardSuites)
{
foreach(string cardvalues in cardValues)
{
deckOfCards.Add(cardvalues + "-" + cardsuit);
}
}
for(int i = 0; i <= 10; i++)
{
List<string> pokerHand = new List<string>();
Random rand = new Random();
deckOfCards = deckOfCards.Select(x => new { card = x, rand = rand.Next() }).OrderBy(x => x.rand).Select(x => x.card).ToList();
for(int x = 0; x < 5; x++)
{
pokerHand.Add(deckOfCards[x]);
}
Console.WriteLine(String.Join(", ", pokerHand));
}
Console.ReadLine();
}
}
}