代码如下:
/// <summary>
/// 返回随机数
/// </summary>
/// <param name="VcodeNum"></param>
/// <returns></returns>
public string RndNum(int VcodeNum)
{
string Vchar = "0,1,2,3,4,5,6,7,8,9";
string[] VcArray = Vchar.Split(',');
string VNum = "";//由于字符串很短,就不用StringBuilder了
int temp = -1;//记录上次随机数值,尽量避免生产几个一样的随机数
//采用一个简单的算法以保证生成随机数的不同
Random rand = new Random();
for (int i = 1; i < VcodeNum + 1; i++)
{
if (temp != -1)
{
rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
}
//int t = rand.Next(35) ;
int t = rand.Next(9);
if (temp != -1 && temp == t)
{
return RndNum(VcodeNum);
}
temp = t;
VNum += VcArray[t];
}
return VNum;
}
调用时只需要 string code = RndNum(4);
这样取得了4位随机数.
如果需要字母随机数,则
string Vchar = "0,1,2,3,4,5,6,7,8,9,A,B";把字母加上去.
同时修改int t = rand.Next(9);中的9改成Vchar的长度但可.