0

I am new to C# and I want to shuffle around my list of strings so no value is input twice and all values are shuffled. So far I have this code

private List<string> myCards = List<string> 
{
    "AC", "AS", "AD", "AH",
    "2C", "2S", "2D", "2H",
    "3C", "3S", "3D", "3H",
    "4C", "4S", "4D", "4H",
    "5C", "5S", "5D", "5H",
    "6C", "6S", "6D", "6H",
    "7C", "7S", "7D", "7H",
    "8C", "8S", "8D", "8H",
    "9C", "9S", "9D", "9H",
    "10C", "10S", "10D", "10H", 
    "KC", "KS", "KD", "KH",
    "QC", "QS", "QD", "QH",
    "JC", "JS", "JD", "JH"
};

Cheers

Tom
  • 2,543
  • 3
  • 21
  • 42

2 Answers2

1

You want to remove duplicates and shuffle?

Install Extended Random from NuGet*, import ExtendedRandom and System.Linq, then:

ExtendedRandom.Random.Shuffle(myCards.Distinct())

* I'm the maintainer of that NuGet package

nemec
  • 1,044
  • 9
  • 35
1

You could do something simple like ordering them randomly using linq.

var shuffled = myCards.Distinct().OrderBy(x =>  System.Guid.NewGuid().ToString()).ToList();

implementing it as a method is simple too.

public List<string> Shuffle(List<string> items)
{
  return items.Distinct().OrderBy(x =>  System.Guid.NewGuid().ToString()).ToList();
}
scartag
  • 17,548
  • 3
  • 48
  • 52