B.j
B.j

Reputation: 59

choose uniqe item from list

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)];
        }

Upvotes: 0

Views: 62

Answers (1)

Wicher Visser
Wicher Visser

Reputation: 1543

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.

Upvotes: 3

Related Questions