java生成随机数(字符串)示例分享
用来生成简单的随机java生成随机数,大小+数字。没特符
package passwords;
import java.util.random;
public class pwdgen {
private random rdseed=new random();
/**
*@param
*length password length;
*@param
*letters boolean non-capital letters combination control;
*@param
*letters_capi boolean capital letters combination control;
*@param
*numbers boolean capital letters combination control;
*/
public string getpwd(int length,boolean letters,boolean letters_capi,boolean numbers){
string res="";
random rd=this.rdseed;
while(length-->0){
char _1dgt=0;
if(letters&&letters_capi&&numbers){
int _key=rd.nextint(3);
if(2==_key){
_1dgt=get_l();
}else if(1==_key){
_1dgt=get_l_c();
}else if(0==_key){
_1dgt=get_n();
}
}else if(letters&&letters_capi&&!numbers){
int _key=rd.nextint(2);
if(1==_key){
_1dgt=get_l();
}else if(0==_key){
_1dgt=get_l_c();
}
}else if(!letters&&letters_capi&&numbers){
int _key=rd.nextint(2);
if(1==_key){
_1dgt=get_n();
}else if(0==_key){
_1dgt=get_l_c();
}
}else if(letters&&!letters_capi&&numbers){
int _key=rd.nextint(2);
if(1==_key){
_1dgt=get_l();
}else if(0==_key){
_1dgt=get_n();
}
}else if(letters&&!letters_capi&&!numbers){
_1dgt=get_l();
}else if(!letters&&!letters_capi&&numbers){
_1dgt=get_n();
}else if(!letters&&letters_capi&&!numbers){
_1dgt=get_l_c();
}else{
res=null;
break;
}
res+=_1dgt;
}
return res ;
}
private char get_l_c(){
random rd=this.rdseed;
int _dgt=(rd.nextint(26)+65);
return (char)_dgt;
}
private char get_n(){
random rd=this.rdseed;
int _dgt=(rd.nextint(10)+48);
return (char)_dgt;
}
private char get_l(){
random rd=this.rdseed;
int _dgt=(rd.nextint(26)+97);
return (char)_dgt;
}
private char get_s(){
random rd=this.rdseed;
int _dgt=(rd.nextint(14)+33);
return (char)_dgt;
}
public static void main(string[] args) {
pwdgen test= new pwdgen();
int testct=1000000;
long start=system.nanotime();
while(testct-->0){
test.getpwd(10, true, true, true);
}
long end= system.nanotime();
system.out.println(((double)(end-start))/(1000000000));
system.out.println(test.getpwd(10, true, true, true));
}
}