0

I have this list of colors in a wpf application. what I want is to random choose a color each time I call the function but I don’t want that a one color be chosen more than on time. This is my code that not doing what I want.

 var polyline = new MapPolyline();
 polyline.Stroke = GetRandomPolylineColor();

private Brush GetRandomPolylineColor()
        {
            var brushes = new Brush[] 
            {       Brushes.Blue,
                 Brushes.Black,
                 Brushes.Red,
                 Brushes.Brown,
                 Brushes.Green,
                 Brushes.HotPink,
                 Brushes.Khaki,
                 Brushes.IndianRed,
                 Brushes.LimeGreen,
                 Brushes.Orange                               
            };

            var rnd = new Random();
            return brushes[rnd.Next(brushes.Length)];
        }
Nahuel Ianni
  • 3,177
  • 4
  • 23
  • 30
B.j
  • 47
  • 1
  • 9
  • Is your code currently swapping the color? or is that also one of the problems? if it changes all you should need is to make a temp variable for ur current brush, then check it for equality and go again if its the same as before – pål-jørgen valdersnes Apr 14 '16 at 07:08
  • Id probably have used lists and had a master list, aka master_brushes and set brushes to the same content, and then each time picked from brushes, removed the used item, on finding 0 items, reload from master and start over again. – BugFinder Apr 14 '16 at 07:15

1 Answers1

3

What you are describing is known as sampling without replacement. This has been answered in the SO posts Unique (non-repeating) random numbers in O(1) and Algorithm for sampling without replacement.

Community
  • 1
  • 1
Wicher Visser
  • 1,513
  • 12
  • 21