In my app every user that connects to the server needs to get a random distinct color and I have to keep track of them. About 20 to 30 will be connected in the same time. When a user disconnects, his color gets dismissed.My question is if I did this correctly concerning performance and thread safety.public class RandomColorGenerator{    private static IList<Color> currentColors = new List<Color>();    private static Random random = new Random((int)DateTime.UtcNow.Ticks);    private object lockObject = new object();    public Color GetRandomColor ( )    {        var color = Color.FromArgb(random.Next(200, 255), random.Next(150, 255), random.Next(150, 255));        lock ( lockObject )        {            if ( currentColors.Contains(color, new ColorEqualityComparer()) )                return GetRandomColor();            else                {                    currentColors.Add(color);                    return color;                }        }    }    public void DismissRandomColor (Color color)    {        lock ( lockObject )        {            var tmpColor = currentColors.SingleOrDefault(x=>x.ToArgb() == color.ToArgb());            currentColors.Remove(tmpColor);        }    }}public class ColorEqualityComparer : IEqualityComparer<System.Drawing.Color>{    public bool Equals (Color x, Color y) => x.ToArgb() == y.ToArgb();    public int GetHashCode (Color obj) => obj.GetHashCode();}EDIT:Thanks everybody for your help and advices, you helped me a lot about this problem and i have also managed to learn a couple of extra stuff :) Since randomness wasn't really that needed, i will just create about 60 random colors offline and store them in an array or a dictionary and just assign them to users as needed.
Generate a random color for every user and keep track of created colors. Every color MUST be different
c#;multithreading
Its important to know how many distinct colours you have available, and how distinct they have to be. For example can one user have pale yellow and another user have slightly paler yellow?If you can have many distinct colours, simply generate them using a deterministic algorithm, starting at 0,0,0 and working your way up to 255,255,255 (this gives you 256*256*256 different colours!). If you need colours to be more distinct, you can still use the fixed algorithm but use a large stepping value - eg increment the value by 64 instead of 1 (which gives you 256/64^3 instead or 64 different colours).Once you have this kind of algorithm you just need to give each user a number. You will have to maintain a list of these numbers but you can either keep a 'boolean array' of each one or simply search through all users looking for the smallest unused number. Alternatively, calculate the colours offline and store the values in an array. Each user gets an index into that array. For a new user simply look through all users for the next unused index, or store a 'used/free' flag alongside the colour and update the array as users come and go. 