Random rand = new Random();
private string CreateRandomCode(int codeCount) //codeCount是希望生成的长度
{
string allChar = "A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,U,V,W,X,Y,Z,2,3,4,5,6,7,8,9" ;
string[] allCharArray = allChar.Split(',');
string randomCode = "";
int temp = -1;
for(int i = 0; i < codeCount; i++)
{
if(temp != -1)
{
rand = new Random(i*temp*((int)DateTime.Now.Ticks));
}
int t = rand.Next(31);
if(temp == t)
{
return CreateRandomCode(codeCount);
}
temp = t;
randomCode += allCharArray[t];
}
return randomCode;
}这是随机生成密码的程序
怎样保证随机生成的密码没有重复的项啊?
谢谢
private string CreateRandomCode(int codeCount) //codeCount是希望生成的长度
{
string allChar = "A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,U,V,W,X,Y,Z,2,3,4,5,6,7,8,9" ;
string[] allCharArray = allChar.Split(',');
string randomCode = "";
int temp = -1;
for(int i = 0; i < codeCount; i++)
{
if(temp != -1)
{
rand = new Random(i*temp*((int)DateTime.Now.Ticks));
}
int t = rand.Next(31);
if(temp == t)
{
return CreateRandomCode(codeCount);
}
temp = t;
randomCode += allCharArray[t];
}
return randomCode;
}这是随机生成密码的程序
怎样保证随机生成的密码没有重复的项啊?
谢谢
解决方案 »
- 关于visual2008中Gridview的问题,如何判断已选中某行
- 重载 继承 多态 抽象类 接口 求解析
- C#操作打印队列一问
- 几个关于简单数据绑定的问题(绑定静态成员等)
- 关于文件操作..请各位指点..
- 怎么样打印datagrid中的数据
- 我是初学者,碰到一个很奇怪的问题,请教老师,万分感谢!
- DataGrid里不用数据连接,怎么样把一个table里的数据显示在datagrid里面啊??
- C#图片从一个框拖到另一个框的理论知识?
- 怎样使一个C#写的应用程式在非.NET上运行?
- 如何实现对于TreeView中的不同树节点有不同的右键菜单,如何实现(等ing online)
- 如果实现MSN风格的关闭效果啊~就是关闭窗体的时候实际上是最小化通过托盘弹出
rand = new Random(i*temp*((int)DateTime.Now.Ticks));
这条语句应该有时间戳的功能啊,怎么生成的密码里面有很多的完全一样 的啊?
Console.WriteLine(DateTime.Now.Ticks);
但是,如果应用程序在一个较快的计算机上运行,则该计算机的系统时钟可能没有时间在此构造函数的调用之间进行更改,Random 的不同实例的种子值可能相同。这种情况下,请应用一个算法来区分每个调用的种子值。例如,以下的 C# 表达式使用按位求补运算来生成两个不同的种子值,即使系统时间值相同也可以。Random rdm1 = newRandom(unchecked((int)DateTime.Now.Ticks)); Random rdm2 = newRandom(~unchecked((int)DateTime.Now.Ticks)); 但是我这样写也不行的啊?
using System.Collections;class PasswordGenerator
{
Random rand = new Random(); static void Main()
{ Hashtable ht = new Hashtable(); PasswordGenerator pw = new PasswordGenerator();
for (int i=0; i < 10000; i++)
{
string s = pw.CreateRandomCode2(6);
Console.WriteLine(s);
if (ht.Contains(s))
{
Console.WriteLine("****something is wrong:" + s);
return;
}
else
ht[s] = "1";
}
} //产生的Code string每个字符都是无重复的
private string CreateRandomCode(int codeCount)
{ char[] allCharArray = {'A','B','C','D','E','F','G','H','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z','2','3','4','5','6','7','8','9'};
int last = allCharArray.Length - 1; for(int i = 0; i < codeCount; i++)
{
int t = rand.Next(last + 1);
char temp = allCharArray[last];
allCharArray[last] = allCharArray[t];
allCharArray[t] = temp; last--;
}
return new String(allCharArray, last+1,codeCount);
}
private string CreateRandomCode2(int codeCount)
{ char[] allCharArray = {'A','B','C','D','E','F','G','H','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z','2','3','4','5','6','7','8','9'};
char[] rc = new char[codeCount];
int j=0;
for(int i = 0; i < codeCount; i++)
{
int t = rand.Next(allCharArray.Length);
rc[j++] = allCharArray[t];
}
return new String(rc, 0,codeCount);
}
}