不知道怎么随即生成大小写的字母,用swith弄了个,只有a-f6个字母的楼主看下,可以的话自己加case吧 期待高手给出好方法 private string suijishu() { Random ro=new Random (); string result = ""; for (int i = 0; i < 16; i++) { if (i>0&&i % 4 == 0) result += "-"; switch (ro.Next(1, 7)) { case 1: result += "a"; break; case 2: result += "b"; break; case 3: result += "c"; break; case 4: result += "d"; break; case 5: result += "e"; break; case 6: result += "f"; break; } } return result; }
private string CreateRandomNum() { string result = ""; #region 生成随机号 for (int i = 0; i < 16; i++) { Random myRandom1 = new Random((int)DateTime.Now.Ticks); System.Threading.Thread.Sleep(10); int kinds = myRandom1.Next(0, 3); switch (kinds) { case 0://数字类 #region 数字 Random myRandom11 = new Random((int)DateTime.Now.Ticks); System.Threading.Thread.Sleep(10); int number11 = myRandom11.Next(0, 9); result += number11.ToString(); continue; #endregion case 1://大写字母类 #region 大写字母类 Random myRandom22 = new Random((int)DateTime.Now.Ticks); System.Threading.Thread.Sleep(10); int number22 = myRandom22.Next(65, 90); result += Convert.ToChar(number22).ToString(); continue; #endregion case 2://小写字母类 #region 小写字母类 Random myRandom33 = new Random((int)DateTime.Now.Ticks); System.Threading.Thread.Sleep(10); int number33 = myRandom33.Next(97, 122); result += Convert.ToChar(number33).ToString(); continue; #endregion default: break; } } #endregion #region 加"-" string newstring=""; for (int i = 1; i <= result.Length; i++) { newstring+=result[i-1]; if (i % 4 == 0 && i != result.Length) newstring += "-"; } result = newstring; #endregion return result; }刚写的随机数如你所说:数字,大,小写字母,都有。
linjf520 谢谢 我试试
这么快就出来了,再送一个简单点的 public class Main { public static void main(String[] args) throws Exception { Main m = new Main(); int max_count = 10;//产生序列个数 int count =max_count; Vector<String> l=new Vector(); char[]ckeys=new char[max_count]; while(count>0){ char [] c = m.getKey(); String s=new String(c); System.out.println(""+s); if(m.checkKey(c)&&!l.contains(s)){//l.contains(s)检查重复序列 l.add(new String(c)); count--; } } for(int i=0;i<l.size();i++){//打印 System.out.println(""+l.get(i)); } } static char[] hc = {'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'}; char[] getKey() {//得到随机序列 char k[] = new char[16]; for (int i = 0; i < 16; i++) { int ind = ((int)( Math.random() * 100000)) % 62; k[i] = hc[ind]; } return k; } boolean checkKey(char[] c) {//检查是否是合格的序列 boolean Nu = true, ABC = true, abc = true; byte ct = 0; for (int i = 0; i < 16; i++) { if (Nu && '0' <= c[i] && c[i] >= '9') { Nu = false; ct++; } if (abc && 'a' <= c[i] && c[i] >= 'z') { abc = false; ct++; } if (ABC && 'A' <= c[i] && c[i] >= 'Z') { ABC = false; ct++; } if (ct > 1) { return true; } } return false; } }
好像也不是啊,那个是固定长度的。
这句话不懂,是否意思为:
if(textbox.Text="5")
//就产生$%^#$^#$*&(&*))#%@#@!的。
区别的就是我的要16位 并且每4位隔开咯
期待高手给出好方法 private string suijishu()
{
Random ro=new Random ();
string result = "";
for (int i = 0; i < 16; i++)
{
if (i>0&&i % 4 == 0)
result += "-";
switch (ro.Next(1, 7))
{
case 1:
result += "a";
break;
case 2:
result += "b";
break;
case 3:
result += "c";
break;
case 4:
result += "d";
break;
case 5:
result += "e";
break;
case 6:
result += "f";
break;
}
}
return result;
}
public string pxkt_GetCharFont(int strLength)
{
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();
}
{
string result = "";
#region 生成随机号
for (int i = 0; i < 16; i++)
{
Random myRandom1 = new Random((int)DateTime.Now.Ticks);
System.Threading.Thread.Sleep(10);
int kinds = myRandom1.Next(0, 3);
switch (kinds)
{
case 0://数字类
#region 数字
Random myRandom11 = new Random((int)DateTime.Now.Ticks);
System.Threading.Thread.Sleep(10);
int number11 = myRandom11.Next(0, 9);
result += number11.ToString();
continue;
#endregion
case 1://大写字母类
#region 大写字母类
Random myRandom22 = new Random((int)DateTime.Now.Ticks);
System.Threading.Thread.Sleep(10);
int number22 = myRandom22.Next(65, 90);
result += Convert.ToChar(number22).ToString();
continue;
#endregion
case 2://小写字母类
#region 小写字母类
Random myRandom33 = new Random((int)DateTime.Now.Ticks);
System.Threading.Thread.Sleep(10);
int number33 = myRandom33.Next(97, 122);
result += Convert.ToChar(number33).ToString();
continue;
#endregion
default: break;
}
}
#endregion
#region 加"-"
string newstring="";
for (int i = 1; i <= result.Length; i++)
{
newstring+=result[i-1];
if (i % 4 == 0 && i != result.Length)
newstring += "-";
}
result = newstring;
#endregion
return result;
}刚写的随机数如你所说:数字,大,小写字母,都有。
谢谢 我试试
public class Main { public static void main(String[] args) throws Exception {
Main m = new Main(); int max_count = 10;//产生序列个数
int count =max_count;
Vector<String> l=new Vector();
char[]ckeys=new char[max_count];
while(count>0){
char [] c = m.getKey();
String s=new String(c);
System.out.println(""+s);
if(m.checkKey(c)&&!l.contains(s)){//l.contains(s)检查重复序列
l.add(new String(c));
count--;
}
}
for(int i=0;i<l.size();i++){//打印
System.out.println(""+l.get(i));
} }
static char[] hc = {'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'}; char[] getKey() {//得到随机序列
char k[] = new char[16];
for (int i = 0; i < 16; i++) {
int ind = ((int)( Math.random() * 100000)) % 62;
k[i] = hc[ind];
}
return k;
} boolean checkKey(char[] c) {//检查是否是合格的序列
boolean Nu = true, ABC = true, abc = true;
byte ct = 0;
for (int i = 0; i < 16; i++) {
if (Nu && '0' <= c[i] && c[i] >= '9') {
Nu = false;
ct++;
}
if (abc && 'a' <= c[i] && c[i] >= 'z') {
abc = false;
ct++;
}
if (ABC && 'A' <= c[i] && c[i] >= 'Z') {
ABC = false;
ct++;
}
if (ct > 1) {
return true;
}
}
return false;
}
}
private void button_click(object sender,eveargs e)
{
this.textbox.text=CreateRandomNum();
}就可以测试了。
int xiaoxiecanshu = 0;
int daxiecanshu = 0;
int shuzicanshu = 0;
private string suijishu()
{
Random ro=new Random ();
string result = "";
for (int i = 0; i < 16; i++)
{
if (i>0&&i % 4 == 0)
result += "-";
if(ro.Next(0, 62)<26)
xiaoxiecanshu++;
else if(ro.Next(0,62)>35)
daxiecanshu++;
else
shuzicanshu++;
result += constant[ro.Next(0, 62)];
}
int canshu = 0;
if (daxiecanshu > 0)
canshu++;
if (xiaoxiecanshu > 0)
canshu++;
if (shuzicanshu > 0)
canshu++;
if (canshu > 1)
return result;
else
return "";
}