-1

So I'm having a bit of a problem with a simple random color generator class that despite all effort will not stop generating the same set of colors everytime the application starts.

This is an issue that only happens the first time it is used. However the time between the initialization of the Random object and the first generation call is user-determined. So I really have no idea what is causing it

Here's my code:

        /// <summary>
    /// Random number generator
    /// </summary>
    static Random Random;

    public static void Initialize()
    {
        //Intializes the random number generator
        Random = new Random();
    }

    /// <summary>
    /// Generates a random color
    /// </summary>
    /// <returns></returns>
    public static Color GenerateOne()
    {
        while (true)
        {
            Color clr = Color.FromArgb(RandByte(), RandByte(), RandByte());

            float sat = clr.GetSaturation();

            if (sat > 0.8 || sat < 0.2)
            {
                continue;
            }

            float brgt = clr.GetBrightness();
            if (brgt < 0.2)
            {
                continue;
            }

            return clr;
        }
    }

    /// <summary>
    /// Generates a set of random colors where the colors differ from each other
    /// </summary>
    /// <param name="count">The amount of colors to generate</param>
    /// <returns></returns>
    public static Color[] GenerateMany(int count)
    {
        Color[] _ = new Color[count];

        for (int i = 0; i < count; i++)
        {
            while (true)
            {
                Color clr = GenerateOne();

                float hue = clr.GetHue();
                foreach (Color o in _)
                {
                    float localHue = o.GetHue();

                    if (hue > localHue - 10 && hue < localHue + 10)
                    {
                        continue;
                    }
                }

                _[i] = clr;
                break;
            }
        }

        return _;

    }

    /// <summary>
    /// Returns a random number between 0 and 255
    /// </summary>
    /// <returns></returns>
    static int RandByte()
    {
        return Random.Next(0x100);
    }
}

Screenshot of repeating color scheme if needed

Thanks in advance :)

Kryxzael
  • 1
  • 2
  • is your program multi-threaded? you use a static `Random` and the class is not thread safe. – Scott Chamberlain May 28 '17 at 20:50
  • 1
    [This](https://stackoverflow.com/a/5264434/3110695) should answer it – FortyTwo May 28 '17 at 20:59
  • The time between the initialization of the Random object and the first generation call is irrelevant. This code should not be producing this result. Please post an [mcve] printing RGB values to the console. (No need for visual output.) – Mike Nakis May 28 '17 at 21:05

1 Answers1

0

Sorry to waste your time guys. Turns out it wasn't the generators fault it was the custom control that used it. Basically the custom control stores it's color set in a property that was overridden by the form designer code (generation happens during property initialization before InitializeComponent()). It was essentially just one [Bindable(false)] attribute away.

So yeah... Thanks for the advice anyway :)

Kryxzael
  • 1
  • 2