-1

I'm attempting to display the contents of a list randomized with and without LINQ.

Normal List Output Example: Prada, Adidas, Levis, Polo, Guess

Randomized List Output Example: Polo, Adidas, Prada, Guess, Levis

        List<string> clothingBrands = new List<string>()
        { "Prada","Adidas","Levis", "Polo","Gucci",
          "Calvin Klein","Aeropostale","Tommy Hilfiger","Puma","American Eagle",
          "Lacoste","Hollister","Guess","Under Armour","Old Navy",
          "Banana Republic","Hugo Boss", "Diesel","Coach","AND1"};


   private static void RandomzeClothingBrands(List<string> clothingBrands)
    {
        Random rnd = new Random();

        int i = 1;
        foreach (string item in clothingBrands)
        {
            Console.WriteLine($"{i}.{item}");
        }
    }

How can I accomplish printing out the contents of the list randomized every time?

Lennart
  • 9,657
  • 16
  • 68
  • 84
NTaveras
  • 73
  • 1
  • 8

1 Answers1

0

To randomise a list in a very trivial way, you could just use random and OrderBy

Note : there are more reliable ways to randomise a list

private static Random _rand = new Random();

...

foreach (string item in clothingBrands.OrderBy(x => _rand.Next()))
{
     Console.WriteLine($"{i}.{item}");
} 
TheGeneral
  • 79,002
  • 9
  • 103
  • 141