日期:2014-05-17 浏览次数:20602 次
using System;
using System.Drawing;
class Test
{
static void Main()
{
for (int i = 0; i <= 100; i++)
{
Color co = GetColor();
Console.WriteLine(co.Name);
}
Console.ReadKey();
}
static Color GetColor()
{
Random random = new Random();
int number = random.Next(1,4);
switch (number)
{
case 1:
return Color.Red;
case 2:
return Color.Black;
default:
return Color.Yellow;
}
}
}
------解决方案--------------------
public string GetRandomColor()
{
Random RandomNum_First = new Random((int)DateTime.Now.Ticks);
// 对于C#的随机数,没什么好说的
System.Threading.Thread.Sleep(RandomNum_First.Next(50));
Random RandomNum_Sencond = new Random((int)DateTime.Now.Ticks);
// 为了在白色背景上显示,尽量生成深色
int int_Red = RandomNum_First.Next(256);
int int_Green = RandomNum_Sencond.Next(256);
int int_Blue = (int_Red + int_Green > 400) ? 0 : 400 - int_Red - int_Green;
int_Blue = (int_Blue > 255) ? 255 : int_Blue;
return Color.FromArgb(int_Red, int_Green, int_Blue).Name;
}
------解决方案--------------------
1楼说的很正确,只需生成0-255的随机整数就可以了,RGB(255,255,255)
对于HTML,#FFFFFF这种颜色,你生成单个的随机16进制数,把他们拼接在一起也行。
------解决方案--------------------
1楼的正解,生成0-255的随机数就可以了
------解决方案--------------------
1楼正解,生成0-255随机数,rgb(255,255,255)