1

I have a question with the List<> datatype in C#. I have a List of colors (List<Color> colores) in a program that I am doing in Windows Forms, but I want that everytime that start the program this list mess up it of random form. I am searching a method as colores.Disort() but this doesn't exist.

Any ideas?

My array to disort is:

Color[] colores = new Color[] { 
    Color.FromArgb(128, 128, 255),
    Color.FromArgb(255, 128, 128),
    Color.FromArgb(255, 192, 128),
    Color.FromArgb(255, 255, 128),
    Color.FromArgb(255, 255, 128),
    Color.MediumPurple};

PD: I don't want a method of multiple lines, just a short method. The colors shouldn't repeat it.

Thanks you for your time!

Hossein Golshani
  • 1,847
  • 5
  • 16
  • 27
calvin11
  • 187
  • 1
  • 17

1 Answers1

3
var rnd = new Random();
var randomColors = colores.OrderBy(color => rnd.Next());

Please consider reading SO Post about effectiveness of this method.

Is using Random and OrderBy a good shuffle algorithm?

bot_insane
  • 2,545
  • 18
  • 40