想用c# 生成一组随机密码,可出现这样的情况:
一组50个随机密码里,前10个一样,然后后12个一样,然后又9个一样的
如果在生成密码的前边加个中断,按F10运行下去,结果就是一组中没有一个重复的...原码如下,求高人帮忙: private void button1_Click(object sender, EventArgs e)
{
label1.Text = "";
string RomPass = "";
string a1 = "";
for (int i = 0; i < 10; i++)
{
RomPass = GetCharFont(18);
a1 += RomPass + "\r";
}
label1.Text = a1;
} //得到数字加英文的随机数,参数:随机数长度,返回值:一个字符串!"
public string GetCharFont(int strLength)
{
char[] constant = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
System.Text.StringBuilder newRandom = new System.Text.StringBuilder(62);
Random rd = new Random();
for (int i = 0; i < strLength; i++)
{
newRandom.Append(constant[rd.Next(62)]);
}
return newRandom.ToString();
}点击按钮执行结果:
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ在 button1_Click 加个中断,按F10运行:
qGVW4bY1JJ4DKZjlsl
TFOIctYyykmQKQnD9c
lHDgreawi2CIC6D4EY
OGv2zxb38DTUCYHmlQ
uK4qHV57NG1jWYt14V
YJWcQe6EChjvWPxjLM
a6cNqusepHaHWl0BUt
2hpNyYfRUa8iztsCEM
MrwqmjE1wUCaSrPgyC
m06gWKMIZaaLvdGgLK
ebkh4ezluD8m8k9hw3
nxL8RKLBLfiD4yUDtk
EY4TRCG6mtBdfCGqip
xd1pSiOuhPeoNq2u23
8MCfsKWbK6MZqbTufb
GWdrZi3QIv9PoFltqG
j4HCd98mhDfTz3ifXG
EwN61LdboFfnOpLYXb
一组50个随机密码里,前10个一样,然后后12个一样,然后又9个一样的
如果在生成密码的前边加个中断,按F10运行下去,结果就是一组中没有一个重复的...原码如下,求高人帮忙: private void button1_Click(object sender, EventArgs e)
{
label1.Text = "";
string RomPass = "";
string a1 = "";
for (int i = 0; i < 10; i++)
{
RomPass = GetCharFont(18);
a1 += RomPass + "\r";
}
label1.Text = a1;
} //得到数字加英文的随机数,参数:随机数长度,返回值:一个字符串!"
public string GetCharFont(int strLength)
{
char[] constant = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
System.Text.StringBuilder newRandom = new System.Text.StringBuilder(62);
Random rd = new Random();
for (int i = 0; i < strLength; i++)
{
newRandom.Append(constant[rd.Next(62)]);
}
return newRandom.ToString();
}点击按钮执行结果:
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ
MfL8kCgpCnn82DeHsJ在 button1_Click 加个中断,按F10运行:
qGVW4bY1JJ4DKZjlsl
TFOIctYyykmQKQnD9c
lHDgreawi2CIC6D4EY
OGv2zxb38DTUCYHmlQ
uK4qHV57NG1jWYt14V
YJWcQe6EChjvWPxjLM
a6cNqusepHaHWl0BUt
2hpNyYfRUa8iztsCEM
MrwqmjE1wUCaSrPgyC
m06gWKMIZaaLvdGgLK
ebkh4ezluD8m8k9hw3
nxL8RKLBLfiD4yUDtk
EY4TRCG6mtBdfCGqip
xd1pSiOuhPeoNq2u23
8MCfsKWbK6MZqbTufb
GWdrZi3QIv9PoFltqG
j4HCd98mhDfTz3ifXG
EwN61LdboFfnOpLYXb
解决方案 »
- listview 读取 xml问题,各位老大来看看
- 这个2个代码和实现功能有区别吗
- 折叠式导航是如何实现的(图)
- C# 重绘button问题,获取button显示文字长度与宽度
- stream 转成 Microsoft.Office.Interop.Word.Document
- 關于Form中引用繼承Form中方法的問題
- C# winform中的DataGrid如何使其行高和列宽不可变
- C# SOCKET 如何实现一个文件从一台电脑指定位置传输到另外一台电能指定位置?????
- *.cab文件是可执行文件吗?
- 小妹请教如果不用循环直接取到XML里的值。
- C#如何实现应用程序的标题保持不变而任务栏里的文字滚动,就像新版本的千千静听一样
- 怎么不会掏出异常??
//得到数字加英文的随机数,参数:随机数长度,返回值:一个字符串!"
public string GetCharFont(int strLength)
{
char[] constant = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
System.Text.StringBuilder newRandom = new System.Text.StringBuilder(62);
DateTime now = DateTime.Now; //需要生成不同的随机数生成器
Random rd = new Random(now.MillSecond * now.MillSecond);
for (int i = 0; i < strLength; i++)
{
newRandom.Append(constant[rd.Next(62)]);
}
return newRandom.ToString();
}
{
newRandom.Append(constant[rd.Next(62)]);
Thread.Sleep(10);//
}
protected void Page_Load(object sender, EventArgs e)
{
string a1 = "";
for (int i = 0; i < 10; i++)
{
string RomPass = GetCharFont(i, 18);
a1 += RomPass + "<br/>";
}
Response.Write(a1);
} //得到数字加英文的随机数,参数:随机数长度,返回值:一个字符串!"
public string GetCharFont(int randKey, int strLength)
{
char[] constant = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
System.Text.StringBuilder newRandom = new System.Text.StringBuilder(62);
Random rd = new Random(randKey);
for (int i = 0; i < strLength; i++)
{
newRandom.Append(constant[rd.Next(62)]);
}
return newRandom.ToString();
} 我改了一下,不过我的平台是webForm所以,有些语句改动了一下。着重点是 Random rd = new Random(randKey);
Random rd = new Random(randKey);
改成
Random rd = new Random(Convert.ToInt32((DateTime.Now.Ticks + randKey) % Int32.MaxValue));
比较正确。
谢谢!!
那这个时候将Random的基础改为一个一定变化的量,才能使结果产生变化。
当然我们在重复、短时间内调用Random的话,一定会用到For 那么将For的循环值继承到Random基础值中,那么产生的结果就一定不同!
Random rd = new Random(Convert.ToInt32((DateTime.Now.Ticks + Rom) % Int32.MaxValue));