-1
所以我有一个简单的随机颜色生成器类的问题,尽管所有的努力不会停止每次应用程序启动时生成相同的颜色集。C# - 随机颜色发生器不工作
这是一个只在第一次使用时才会出现的问题。 但是 Random对象初始化和第一代调用之间的时间为用户确定的。所以,我真的不知道是什么原因造成
这里是我的代码:
/// <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
感谢提前:)
是您的程序多线程?你使用一个静态的'Random',并且这个类不是线程安全的。 –
[This](https://stackoverflow.com/a/5264434/3110695)应该回答它 – FortyTwo
Random对象的初始化和第一代调用之间的时间是不相关的。这段代码不应该产生这个结果。请将[mcve]打印RGB值发布到控制台。 (不需要视觉输出。) –