Java Generate random Number (string) sample sharing _java

Source: Internet
Author: User

Used to generate random numbers, sizes, and numbers for simple random Java generation. No special characters.

Copy Code code as follows:

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 (True, true, true);
}
Long end= system.nanotime ();
System.out.println ((double) (End-start))/(1000000000));
System.out.println (Test.getpwd (True, True, true));
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.