问题产生随机颜色 - asp.net和c# [英] Problem in generating random colors - asp.net and c#

查看:105
本文介绍了问题产生随机颜色 - asp.net和c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要生成产生在我的asp.net应用程序绘制的曲线图的十六进制值随机颜色。

I need to generate to generate random colors in Hex values in my asp.net application to draw a graph .

 Random random = new Random();
 color = String.Format("#{0:X6}", random.Next(0x1000000)); 

以上code生成随机颜色codeS。但我的问题是一段时间内它几乎产生相似的颜色到previous颜色。由于我使用它的图形目的,我需要产生完全不同的颜色。任何想法....

The above code generates random color codes . But my problem is some time it generate almost similar colors to its previous colors. Because of i am using it for graph purpose i need to generate completely different colors. Any ideas....

推荐答案

我可能误解了这个问题......结果
如果该问题,以避免类似的序列的一段时间内产生的颜色的,请参阅KMan的回应,我认为是第一个建议,通过生产各种随机值出同发生器(而不是生产一种新型发电机每次),一个可避免与相同的种子作为pviously用于发电机$ p $制造发电机的风险。

I may have misunderstood the question...
If the issue to avoid similar sequences of colors produced over time, see KMan's response which I think was the first one to suggest that by producing all random values out of the same generator (rather than producing one new generator each time), one avoids the risk of producing a generator with the same seed as a generator previously used.

如果该问题是要避免连续拉两个相似的色彩,下面的反应应该做的。在一排避免两种相似的颜色就意味着是

If the concern is to avoid drawing two "similar" colors in a row, the following response should do. Avoiding two similar colors in a row implies either


  • 使用一些数理逻辑的(但该函数使用不会覆盖所有可能的颜色作为一个体面的随机数发生器将频谱风险)

  • 绘制真正的随机颜色,但拒绝他们(和重新尝试),当他们被视为相似。

第二种方法是在下面的代码片段所示。结果
风格是手写,且颜色验收标准是有点武断,但是这应该提供一个不错的主意。结果
此外,通过重复使用一个随机数发生器(RNG),一个避免以某种方式被RNG每次创建重复序列的风险,偶然使用了相同的种子...

The second approach is what is illustrated in the following snippet.
The style is longhand, and the color acceptance criteria is somewhat arbitrary but this should provide a good idea.
Furthermore, by reusing a single Random Number Generator (RNG), one avoid the risk of repeating sequences if somehow the RNG was created each time, and by chance the same seed was used...

  const int minTotalDiff = 200;    // parameter used in new color acceptance criteria
  const int okSingleDiff = 100;    // id.

  int prevR, prevG, prevB;  // R, G and B components of the previously issued color.
  Random RandGen = null;

  public string GetNewColor()
  {
      int newR, newG, newB;

      if (RandGen == null)
      {
          RandGen = new Random();
          prevR = prevG = prevB = 0;
      }

      bool found = false;
      while (!found)
      {
          newR = RandGen.Next(255);
          newG = RandGen.Next(255);
          newB = RandGen.Next(255);

          int diffR = Math.Abs(prevR - newR);
          int diffG = Math.Abs(prevG - newG);
          int diffB = Math.Abs(prevB - newB);

          // we only take the new color if...
          //   Collectively the color components are changed by a certain
          //   minimum
          //   or if at least one individual colors is changed by "a lot".
          if (diffR + diffG + diffB >= minTotalDiff
              || diffR >= okSingleDiff
              || diffR >= okSingleDiff
              || diffR >= okSingleDiff
          )
            found = true;
        }

       prevR = newR;
       prevG = newG;
       prevB = newB;

      return String.Format("#{0:X2}{0:X2}{0:X2}", prevR, prevG, prevB);
  }

这篇关于问题产生随机颜色 - asp.net和c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆