各位大大 这种现象是个神马情况??第一次发帖!帮忙解决一下!
闲来无事写了一个自动注册的小程序。但是结果却有点小意外。先贴代码
protected void Button1_Click(object sender, EventArgs e)
{
// 得到输入的次数
int num = int.Parse(this.txNum.Text.Trim());
//得到毫秒数
int time1 = DateTime.Now.Millisecond;
//将时间显示在页面上 格式为:yy-mm-dd hh:mm:ss:mm显示到毫秒数
this.lbltime1.Text = DateTime.Now + ":" + time1.ToString();
for (int i = 0; i < num; i++)
{
user user1 = new user();
Random rd = new Random();
user1.Name = Createname(rd.Next(6, 10));
user1.Pwd = Createname(rd.Next(8, 16));
user1.Qq = Createqq(rd.Next(5, 10));
user1.Time = DateTime.Now;
reg(user1);
}
int time2 = DateTime.Now.Millisecond;
this.lbltime2.Text = DateTime.Now + ":" + time2.ToString();
}
private void reg(user user)
{
string strconn = "server=.;database=autoreg;uid=sa;pwd=sa2005";
SqlConnection conn = new SqlConnection(strconn);
conn.Open();
string sql = "insert into users values (@name,@pwd,@QQ,@time)";
SqlParameter[] parm = new SqlParameter[]
{
new SqlParameter("@name",user.Name),
new SqlParameter("@pwd",user.Pwd),
new SqlParameter("@QQ",user.Qq),
new SqlParameter("@time",user.Time)
};
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddRange(parm);
int newid = cmd.ExecuteNonQuery();
conn.Close();
}
private string Createname(int num)
{ //定义用于验证码的字符数组
char[] AllCheckCodeArray ={ '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'};
//定义验证码字符串
string name = "";
Random rd = new Random();
//生成4位验证码字符串
for (int i = 0; i < num; i++)
name += AllCheckCodeArray[rd.Next(AllCheckCodeArray.Length)];
return name;
}
private int Createqq(int num)
{
char[] qqnum ={ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
string qq1 = "";
Random rd = new Random();
for (int i = 0; i < num; i++)
{
qq1 += qqnum[rd.Next(qqnum.Length)];
}
int qq = int.Parse(qq1);
return qq;
}
27,laEs9NuNj,laEs9NuNjJdq,752813,2010-08-27 16:35:01.813
存在大量的重复情况。难道是Random的性能没得for循环的强悍!??谁能解释一下 或者给一个解决方案啊!
或者说是给一个注册机器人的实现方案??谢谢 第一次发帖!
[最优解释]
这是我根据4 楼的提示 写的代码
for (int i = 0; i < num; i++)
{
user user1 = new user();
user1.Name = Createname(rd.Next(6, 10));
user1.Pwd = Createname(rd.Next(8, 16));
user1.Qq = Createqq(rd.Next(5, 10));
user1.Time = DateTime.Now;
reg(user1);
Thread.Sleep(50);
}
加了一句让程序暂停 一下 但是结果也不咋的
13odn01ts3odn01tsPIZ6K86702010-08-27 17:29:46.953
2yJgES57TyJgES57TCWyH936240142010-08-27 17:29:47.017
3MU0TRGuaMU0TRGua34044292010-08-27 17:29:47.077
4Gz3utJGz3utJ8Cfc4sZ2909832010-08-27 17:29:47.140
5fAO9sVuILfAO9sVuILU61318592010-08-27 17:29:47.203
6ZfQbKY8uZfQbKY8u8iATVh564535192010-08-27 17:29:47.267
7xqlpJjvxqlpJjv0nZq987837902010-08-27 17:29:47.327
8sLnHln9csLnHln9cboF3R83727712010-08-27 17:29:47.390
9GW8VkyvGW8Vkyvi2515799722010-08-27 17:29:47.453
10B0AxC19K3B0AxC19K3u1019102010-08-27 17:29:47.517
11ZCVBBDvQZCVBBDvQjl1525112010-08-27 17:29:47.577
12UgXddGA2UgXddGA246566212010-08-27 17:29:47.640
13sssscRwsssscRw8Cr8888649112010-08-27 17:29:47.703
14mMuJ4VAkmMuJ4VAkz5WpEp7393051792010-08-27 17:29:47.767
15BYFY3gwqfBYFY3gwqf15250692010-08-27 17:29:47.827
1652I0VkAS52I0VkASSBcPArZ20571442010-08-27 17:29:47.890
17TEdEUvxTEdEUvxY72426249952010-08-27 17:29:47.953
18OifgwyBAvOifgwyBAvHi063766991192010-08-27 17:29:48.017
19mu0uv9xmu0uv9xGa8OPi9S790992010-08-27 17:29:48.077
20hO2MNDBhO2MNDBsNNn64033212010-08-27 17:29:48.140
215aNaMOyy35aNaMOyy3ETze53533992010-08-27 17:29:48.203
2204P2oS04P2oSCaqStBx40842010-08-27 17:29:48.267
23OGkHndOGkHndyhW3272862010-08-27 17:29:48.327
24IkmiFgIkmiFgCIJY37772622010-08-27 17:29:48.390
25hw7xFsyPzhw7xFsyPzQfAV691928942010-08-27 17:29:48.453
26bQ9OgvD0mbQ9OgvD0me4Mp1364146922010-08-27 17:29:48.517
27zcUdg6zcUdg6z7RWklR964662010-08-27 17:29:48.577
28u6X57ADiFu6X57ADiFkAw915012010-08-27 17:29:48.640
29IIsJ7LzpIIsJ7LzpubqMNJ338313982010-08-27 17:29:48.703
30DmulYODDmulYODQhq279752010-08-27 17:29:48.767
31byFzYa0XbyFzYa0XNhwwJL6929550532010-08-27 17:29:48.827
32WSHRzdEWSHRzdE8AwL8c7542492010-08-27 17:29:48.890
33uecfzo0uecfzo0Fqn1XEN96669802010-08-27 17:29:48.953
34o8e7Qso8e7QsEqd1R8161482862010-08-27 17:29:49.017
35DKzMQ31xDKzMQ31xJt2393400932010-08-27 17:29:49.077
367o1nr77o1nr7FY67XJTBX180882010-08-27 17:29:49.140
37V0M2rIV0M2rI1flzDi503082010-08-27 17:29:49.203
38QUOUILFQUOUILFGZD444433222010-08-27 17:29:49.267
39ogjiIX2ogjiIX2NE5II2TQ867722010-08-27 17:29:49.327
40jAmAjaGzjAmAjaGz1J7171752902010-08-27 17:29:49.390
417M6Ojl257M6Ojl25hAOtxVv1313770072010-08-27 17:29:49.453
422q9qAp2q9qApGhUPo48181822010-08-27 17:29:49.517
43Q2U4A02nQ2U4A02nAGUT4040100712010-08-27 17:29:49.577
44KWWWb3HPKWWWb3HPxUtf355562010-08-27 17:29:49.640
45jirlbF3VdjirlbF3VdMZ4pZ7787522010-08-27 17:29:49.703
46dCtC2IHdCtC2IH7Qa6282032010-08-27 17:29:49.767
471OER2T1OER2T3D5Sf324040202010-08-27 17:29:49.827
48wsGsTXHpwsGsTXHp9828452882010-08-27 17:29:49.890
49K4b7TiK4b7Ti4v30614702010-08-27 17:29:49.953
50FYdZumIFYdZumIXLmAQzP02565972532010-08-27 17:29:50.017
这个有一个弊端就是时间太长 然后就是密码是在用户名的基础上加了一串字符串组成的。。。
[其他解释]
写在外面
[其他解释]
Random rad = new Random((int)(DateTime.Now.Ticks & 0xffffffffL)
[其他解释]
(int)(DateTime.Now.Ticks >> 32));
显示结果如下:
1obr8Lriobr8Lrik3AWq8681387702010-08-27 17:25:33.970
2obr8Lriobr8Lrik8681382010-08-27 17:25:33.970
3obr8Lrik3obr8Lrik3AWqD868132010-08-27 17:25:33.970
4obr8Lriobr8Lrik3AWqDb8681382010-08-27 17:25:33.970
5obr8Lriobr8Lrik3AWqD8681382010-08-27 17:25:33.970
6obr8Lrobr8Lrik3AWqD868132010-08-27 17:25:33.970
7obr8Lriobr8Lrik3AWq8681387702010-08-27 17:25:33.970
8obr8Lrikobr8Lrik3AWqDbJ8681382010-08-27 17:25:33.970
9obr8Lrik3obr8Lrik3AWqDbJ8681382010-08-27 17:25:33.970
10obr8Lrik3obr8Lrik3AWq868138772010-08-27 17:25:33.970
11obr8Lrikobr8Lrik3AWq86813872010-08-27 17:25:33.970
12obr8Lriobr8Lrik38681382010-08-27 17:25:33.970
13obr8Lriobr8Lrik3AW86813872010-08-27 17:25:33.970
14obr8Lrikobr8Lrik3AW868138772010-08-27 17:25:33.970
15obr8Lrik3obr8Lrik868132010-08-27 17:25:33.970
16obr8Lrobr8Lrik3AWqDbJ86813872010-08-27 17:25:33.970
17obr8Lrobr8Lrik3AWqDbJ8681382010-08-27 17:25:33.970
18obr8Lriobr8Lrik3AW130552010-08-27 17:25:33.983
19AI2YZqtm2AI2YZqtm24JKDv130558872010-08-27 17:25:33.983
20AI2YZqtAI2YZqtm24J130552010-08-27 17:25:33.983
21AI2YZqtm2AI2YZqtm24JKDvV130552010-08-27 17:25:33.983
22AI2YZqtAI2YZqtm24JK1305582010-08-27 17:25:33.983
23AI2YZqtm2AI2YZqtm24JKDvV1305582010-08-27 17:25:33.983
24AI2YZqAI2YZqtm24JKDvV130552010-08-27 17:25:33.983
25AI2YZqtmAI2YZqtm24JKDv1305582010-08-27 17:25:33.983
26AI2YZqAI2YZqtm24JK1305588702010-08-27 17:25:33.983
27AI2YZqAI2YZqtm24JKD130552010-08-27 17:25:33.983
28AI2YZqtmAI2YZqtm24JKDvV13055882010-08-27 17:25:33.983
29AI2YZqtAI2YZqtm24130552010-08-27 17:25:33.983
30AI2YZqtAI2YZqtm24JK13055882010-08-27 17:25:33.983
31AI2YZqtmAI2YZqtm24JKDv130552010-08-27 17:25:33.983
32AI2YZqtAI2YZqtm24JK13055882010-08-27 17:25:33.983
33AI2YZqtmAI2YZqtm24J13055882010-08-27 17:25:33.983
34AI2YZqtm2AI2YZqtm241305588702010-08-27 17:25:33.983
35AI2YZqAI2YZqtm24JKDvV130552010-08-27 17:25:33.983
36AI2YZqAI2YZqtm24JKD130558872010-08-27 17:25:33.983
37AI2YZqtm2AI2YZqtm24JK130552010-08-27 17:25:33.983
38AI2YZqtmAI2YZqtm24130558872010-08-27 17:25:33.983
39AI2YZqAI2YZqtm24130552010-08-27 17:25:33.983
40AI2YZqtmAI2YZqtm1305582010-08-27 17:25:33.983
41AI2YZqAI2YZqtm24JK1305582010-08-27 17:25:33.983
42AI2YZqtm2AI2YZqtm24130552010-08-27 17:25:33.983
43AI2YZqAI2YZqtm24J1305582010-08-27 17:25:33.983
44AI2YZqtmAI2YZqtm24JKD130552010-08-27 17:25:33.983
45AI2YZqtAI2YZqtm21305582010-08-27 17:25:33.983
46AI2YZqAI2YZqtm24JKD1305588702010-08-27 17:25:33.983
47AI2YZqtmAI2YZqtm24130558872010-08-27 17:25:33.983
48AI2YZqtAI2YZqtm24J1305588702010-08-27 17:25:33.983
49AI2YZqtAI2YZqtm13055882010-08-27 17:25:33.983
50AI2YZqtAI2YZqtm21305582010-08-27 17:25:33.983
[其他解释]
没人????
[其他解释]
是时间问题 你的随机数种子的问题
[其他解释]
按照这个方法测试了一下 结果如下:
1x1sTNqu0nx1sTNqu09084389072010-08-27 17:16:14.877
2x1sTNqux1sTNqu0ndu9084389072010-08-27 17:16:14.877
3x1sTNqu0nx1sTNqu0908438902010-08-27 17:16:14.877
4Ji3tbp42Ji3tbp42mXhZSKR370862010-08-27 17:16:14.890
5Ji3tbp4Ji3tbp423708682010-08-27 17:16:14.890
6Ji3tbpJi3tbp42mX3708682010-08-27 17:16:14.890
7Ji3tbp42mJi3tbp42mXhZ37086802010-08-27 17:16:14.890
8Ji3tbpJi3tbp42mXhZS3708682010-08-27 17:16:14.890
9Ji3tbp42Ji3tbp42m3708680072010-08-27 17:16:14.890
10Ji3tbpJi3tbp42mX3708682010-08-27 17:16:14.890
11Ji3tbp42Ji3tbp42mX3708680072010-08-27 17:16:14.890
12Ji3tbp42Ji3tbp42mX3708682010-08-27 17:16:14.890
13Ji3tbp4Ji3tbp42mX3708680072010-08-27 17:16:14.890
14Ji3tbp4Ji3tbp42mX3708682010-08-27 17:16:14.890
15Ji3tbp4Ji3tbp42mX3708682010-08-27 17:16:14.890
16Ji3tbpJi3tbp42mXhZ37086802010-08-27 17:16:14.890
17Ji3tbp4Ji3tbp42mXhZ37086802010-08-27 17:16:14.890
18Ji3tbpJi3tbp423708680072010-08-27 17:16:14.890
19Ji3tbp42Ji3tbp42mXhZSK3708682010-08-27 17:16:14.890
20Ji3tbp42Ji3tbp42mXhZS3708682010-08-27 17:16:14.890
21Ji3tbp42mJi3tbp42mXhZSK370868002010-08-27 17:16:14.890
22Ji3tbp4Ji3tbp42370868002010-08-27 17:16:14.890
23Ji3tbpJi3tbp42mXhZSKR370868002010-08-27 17:16:14.890
24Ji3tbp42mJi3tbp42mXhZS3708680072010-08-27 17:16:14.890
25Ji3tbp4Ji3tbp42mXh3708682010-08-27 17:16:14.890
26Ji3tbpJi3tbp42mXhZS37086802010-08-27 17:16:14.890
27Ji3tbpJi3tbp42mXhZ37086802010-08-27 17:16:14.890
28Ji3tbp4Ji3tbp42mXhZS3708680072010-08-27 17:16:14.890
29Ji3tbp42mJi3tbp42mXh3708682010-08-27 17:16:14.890
30Ji3tbpJi3tbp42mXhZSKR37086802010-08-27 17:16:14.890
31Ji3tbp42mJi3tbp42mXhZSKR370868002010-08-27 17:16:14.890
32Ji3tbp42Ji3tbp42mXhZS370868002010-08-27 17:16:14.890
33Ji3tbp42Ji3tbp423708682010-08-27 17:16:14.890
34Ji3tbp42Ji3tbp42370868002010-08-27 17:16:14.890
35fPEJpoF4lfPEJpoF4lRU6423882072010-08-27 17:16:14.907
36fPEJpoF4fPEJpoF4lRU3Se642388202010-08-27 17:16:14.907
37fPEJpoF4fPEJpoF4lRU642382010-08-27 17:16:14.907
38fPEJpoF4lfPEJpoF4lRU36423882010-08-27 17:16:14.907
39fPEJpoF4fPEJpoF4lR642382010-08-27 17:16:14.907
40fPEJpoF4lfPEJpoF4lRU3642388202010-08-27 17:16:14.907
41fPEJpoFfPEJpoF46423882072010-08-27 17:16:14.907
42fPEJpoFfPEJpoF4642388202010-08-27 17:16:14.907
43fPEJpoFfPEJpoF4lRU3642388202010-08-27 17:16:14.907
44fPEJpoF4fPEJpoF4lRU3642388202010-08-27 17:16:14.907
45fPEJpofPEJpoF4lR6423882010-08-27 17:16:14.907
46fPEJpoF4lfPEJpoF4lRU3S6423882010-08-27 17:16:14.907
47fPEJpoF4lfPEJpoF4lR64238822010-08-27 17:16:14.907
48fPEJpoF4fPEJpoF4lRU64238822010-08-27 17:16:14.907
49fPEJpoFfPEJpoF4l64238822010-08-27 17:16:14.907
50fPEJpoFfPEJpoF46423882010-08-27 17:16:14.907
感觉还是怪怪的。。。不是我要的那种结果!
[其他解释]