强类型.List<Int32> list = new List<int>(); int iCount = 0; int iMaxCount = 5; Random r = new Random(); while (iCount < iMaxCount) { int iData = r.Next(100); if(!list.Contains(iData)) { list.Add(iData); iCount++; } Application.DoEvents(); }
//输出 string strData = string.Empty; for (int i = 0; i < iMaxCount; i++) { strData = strData + list[i].ToString() + "\\"; } MessageBox.Show(strData);在C# + .Net2.0通过.
public string GetRandom(string Base, int Length) { string RandomStr = ""; Random rand = new Random(); while (RandomStr.Length < StrLength) { RandomStr = RandomStr + Base[rand.Next(Base.Length)]; } return RandomStr; }public string GetRandom(int Length) { return GetRandom("0123456789",Length); }这里的string Base放的是你所要产生的随机数的字符集合,比如要包含0123456789这几个数字,或者包含"abcdefghizklmn"都可以。
这个返回的是个BOOL值吧?
http://blog.csdn.net/vrhero/archive/2007/07/29/1714682.aspx
Random r = new Random();
while (iCount < iMaxCount)
{
int iData = r.Next(100);
if(!list.Contains(iData))
{
list.Add(iData);
iCount++;
}
Application.DoEvents();
}
//输出
string strData = string.Empty;
for (int i = 0; i < iMaxCount; i++)
{
strData = strData + list[i].ToString() + "\\";
}
MessageBox.Show(strData);在C# + .Net2.0通过.
{
string RandomStr = "";
Random rand = new Random();
while (RandomStr.Length < StrLength)
{
RandomStr = RandomStr + Base[rand.Next(Base.Length)];
}
return RandomStr;
}public string GetRandom(int Length)
{
return GetRandom("0123456789",Length);
}这里的string Base放的是你所要产生的随机数的字符集合,比如要包含0123456789这几个数字,或者包含"abcdefghizklmn"都可以。
for (int i=0; i<5; i++)
{
Console.WriteLine(GetRandom(6));
}这样就可以了。
如果需要int,则int rnd = int.Parse(GetRandom(6));