1 public string GetRandomColor() 3 { 4 5 Random RandomNum_First = new Random((int)DateTime.Now.Ticks); 6 7 // 对于C#的随机数,没什么好说的 8 9 System.Threading.Thread.Sleep(RandomNum_First.Next(50)); 10 11 Random RandomNum_Sencond = new Random((int)DateTime.Now.Ticks); 12 13 14 15 // 为了在白色背景上显示,尽量生成深色 16 17 int int_Red = RandomNum_First.Next(256); 18 19 int int_Green = RandomNum_Sencond.Next(256); 20 21 int int_Blue = (int_Red + int_Green > 400) ? 0 : 400 - int_Red - int_Green; 22 23 int_Blue = (int_Blue > 255) ? 255 : int_Blue; 24 Color color = Color.FromArgb(int_Red, int_Green, int_Blue); 25 string strColor = "#" + Convert.ToString(color.ToArgb(), 16).PadLeft(8, '0').Substring(2, 6); 26 return strColor; 27 28 } 29 30 31