Asp.net生成随机不重复的函数,方法

// 生成三位毫秒字串

public static string Get_mSec()

{

string mSec = System.DateTime.Now.Millisecond.ToString();

mSec = "00" + mSec;

return mSec.Substring(mSec.Length - 3, 3);

}

// 生成 xLen 位随机字串 xRandom 为不同序号(作为种子数)

public static string Rnd_ID4(int xLen, int xRandom)

{

string oStr = "";

int nSeed = int.Parse(System.DateTime.Now.Ticks.ToString().Substring(8, 8));

double dSeed = Math.Sqrt(xRandom + 123.456) + Math.Sqrt(nSeed);

nSeed = (int)(2345678 * Math.Sqrt(dSeed));

System.Random ran = new System.Random(nSeed);

for (int i = 0; i < System.Math.Abs(xLen); i++)

{

int rin = ran.Next(0, 33 - 1);

oStr += ("0123456789ABCDEFGHJKLMNPQRSTUVWXY").Substring(rin, 1);

}

return oStr;

}

// 使用方法:

Response.Write(" <br>- ");

string StrA = "(Peace)乱码人生<br>";

string StrB = "";

for (i = 0; i < 320; i++)

{

StrB = Get_mSec() + "-" + Rnd_ID4(8, i);

if (StrA.IndexOf(StrB) >= 0)

{

Response.Write("<br>i=" + i.ToString() + " : (重复) - " + "- " + StrB);

}

else

{

StrA += StrB + "<BR>";

}

}

Response.Write(" <br><br><br> ");

Response.Write(StrA);

Response.Write(" <br><br><br> ");