惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

A
About on SuperTechFans
Y
Y Combinator Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Microsoft Security Blog
Microsoft Security Blog
aimingoo的专栏
aimingoo的专栏
I
InfoQ
C
Check Point Blog
IT之家
IT之家
MyScale Blog
MyScale Blog
Apple Machine Learning Research
Apple Machine Learning Research
Vercel News
Vercel News
Last Week in AI
Last Week in AI
GbyAI
GbyAI
P
Proofpoint News Feed
量子位
Stack Overflow Blog
Stack Overflow Blog
Microsoft Azure Blog
Microsoft Azure Blog
月光博客
月光博客
阮一峰的网络日志
阮一峰的网络日志
人人都是产品经理
人人都是产品经理
B
Blog
T
The Blog of Author Tim Ferriss
H
Help Net Security
云风的 BLOG
云风的 BLOG

博客园 - Jason.Yi

远程机器上无法用Assembly.Load(path).CreateInstance(ClassName)? - Jason.Yi - 博客园 vs2005代码区出现问题 吉他自录之《回忆组曲》 吉他自录之《爱的罗曼史》 求助:Vs2005 asp.net development server未能开始侦听端口 为什么我的Cookie只能更新一个键值? 对Graphics.DrawImageUnscaledAndClipped的疑虑 哪些原因可能导致SQL操作操时呢? 二个算法题,大家有兴趣来看看 关于博客园的Calendar 费解的.net2.0中的ObjectDataSource控件 FormView在什么情况下自动生成模板项? 错误:已在多处定义 为什么本地sqlservr.exe进程占用内存如此之大? 这样的页面该如何实现? 简单的生成登录验证码的程序 遇到一个很奇怪的问题 连接SQL Server2005出错 能在web.config里做这样的配置吗?
生成随机的颜色值
Jason.Yi · 2006-03-13 · via 博客园 - Jason.Yi

现在要随机生成一组RGB值,如:#FFFFFF
要点是随机生成6个字符,这6个字符从0-9,A-F中取.然后与#连接构成颜色值.

public static string CreateRandomColor()
  {
   string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F";
   string[] allCharArray = allChar.Split(',');
   string randomCode = "";
   int temp = -1;

   Random rand = new Random();
   for(int i = 0; i < 6; i++)
   {
    if(temp != -1)
    {
     rand = new Random(i*temp*((int)DateTime.Now.Ticks));
    }
    int t = rand.Next(15);
    if(temp == t)
    {
     return CreateRandomColor();
    }
    temp = t;
    randomCode += allCharArray[t];
   }
   return "#"+randomCode;
  }

调试:

正确,

循环10次

for(int i=0;i<=10;i++)
   {
    Console.WriteLine(CreateRandomColor());
   }

却重复了,根本没有达到随机的效果。怎么回事?

[STAThread]
        
static void Main(string[] args)
        {
            
//
            
// TODO: 在此处添加代码以启动应用程序
            
//
            string str1 = CreateRandomColor();
            
string str2;
            
for(int i=0;i<=10;i++)
            {
                str2 
= CreateRandomColor();
                
if(str2!=str1)
                {
                    Console.WriteLine(str2);
                    str2
=str1;
                }
            }
            
    }
        
public static string CreateRandomColor()
        {
            
string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F";
            
string[] allCharArray = allChar.Split(',');
            
string randomCode = "";
            
int temp = -1;

            Random rand 

= new Random();
            
for(int i = 0; i < 6; i++)
            {
                
if(temp != -1)
                {
                    rand 
= new Random(i*temp*((int)DateTime.Now.Ticks));
                }
                
int t = rand.Next(15);
                
if(temp == t)
                {
                    
return CreateRandomColor();
                }
                temp 
= t;
                randomCode 
+= allCharArray[t];
            }
            
return "#"+randomCode;
        }