Generating a random alphanumeric string
Hello, can anyone show me an example or give me a clue about how to generate a random alphanumeric string of lets say 16-32 chars? TomCat is obviously doing it for the session ids, but how would I do the same in a servlet? Regards, Christopher Cato - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, email: [EMAIL PROTECTED]
ODP: Generating a random alphanumeric string
see tomcat sources, org.apache.tomcat.util.SessionIdGenerator namely vVolf -Oryginalna wiadomooe- Od: Cato, Christopher [mailto:[EMAIL PROTECTED]] Wysano: 1 marca 2001 15:05 Do: '[EMAIL PROTECTED]' Temat: Generating a random alphanumeric string Hello, can anyone show me an example or give me a clue about how to generate a random alphanumeric string of lets say 16-32 chars? TomCat is obviously doing it for the session ids, but how would I do the same in a servlet? Regards, Christopher Cato - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, email: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, email: [EMAIL PROTECTED]
RE: Generating a random alphanumeric string
Cato, Here is code that I tried out for generating a user id using the user name I have to pass a string and it returns a 8 digit alpha numeric Hope it helps, Anil public static String getCode(String text){ String code = ""; text = text.trim(); if(text.length()1){ return null; } text = text.toUpperCase(); if(random==null){ random = new java.util.Random(); } String code_temp = ""; int no_of_chars = 3 ; //not of chars from text for(int i=0;ino_of_chars;i++){ code_temp = String.valueOf(text.charAt(random.nextInt(text.length(; if(code_temp.equals(" ")){ i--; }else{ code = code + code_temp; } } code = code + java.lang.String.valueOf(random.nextInt(1)); java.util.Date date = new java.util.Date(); code = code + java.lang.String.valueOf(date.getDate()); }catch(Exception e){ return null; } return code; } - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, email: [EMAIL PROTECTED]
RE: Generating a random alphanumeric string
This seems ok for the job. There should be some speedups if you need them. Cheers // --- cut --- public class GenID { private static String validChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; private int _IDlength; public GenID(int IDlength) { _IDlength = IDlength; } public String generate() { String resultID = ""; int maxIndex = validChars.length(); java.util.Random rnd = new java.util.Random(); for ( int i = 0 ; i _IDlength ; i++ ) { int rndPos = Math.abs(rnd.nextInt() % maxIndex); resultID += validChars.charAt(rndPos); } return resultID; } } // --- cut --- -Original Message- From: Cato, Christopher [mailto:[EMAIL PROTECTED]] Sent: Thursday, March 01, 2001 2:05 PM To: '[EMAIL PROTECTED]' Subject: Generating a random alphanumeric string Hello, can anyone show me an example or give me a clue about how to generate a random alphanumeric string of lets say 16-32 chars? TomCat is obviously doing it for the session ids, but how would I do the same in a servlet? Regards, Christopher Cato - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, email: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, email: [EMAIL PROTECTED]
RE: Generating a random alphanumeric string
i did this a few weeks ago; the code is far from "clean'n'nice", but it does what it's supposed to, here goes: Random pwdGen = new Random( System.currentTimeMillis() ); Character temp = new Character( 'a' ); StringBuffer pwd = new StringBuffer(); Vector charPwd = new Vector(); int i = 0; //0-9 for ( i = 48; 57 = i; i++ ) { temp = new Character( ( char ) i ); charPwd.addElement( temp ); } //a-z A-Z for ( i = 65; 90 = i; i++ ) { temp = new Character( ( char ) i ); charPwd.addElement( temp ); } for ( i = 97; 122 = i; i++ ) { temp = new Character( ( char ) i ); charPwd.addElement( temp ); } //where i == length of alphanumeric string. for ( i = 0; 10 i; i++ ) { pwd.append( ( Character ) charPwd.elementAt( ( int ) ( charPwd.size() * pwdGen.nextFloat() ) ) ); } //adrian. -Original Message- From: Cato, Christopher [mailto:[EMAIL PROTECTED]] Sent: 01 March 2001 15:05 To: '[EMAIL PROTECTED]' Subject: Generating a random alphanumeric string Hello, can anyone show me an example or give me a clue about how to generate a random alphanumeric string of lets say 16-32 chars? TomCat is obviously doing it for the session ids, but how would I do the same in a servlet? Regards, Christopher Cato - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, email: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, email: [EMAIL PROTECTED]
RE: Generating a random alphanumeric string
This intrigued me so I just wrote this little program. Of course it could be wrapped up in a bean and it needs to check against a list of established passwords but it gives you upper-lower case random sequence of alpha-numeric characters. Just feed it the length that you want. It even cleans up after itself. :0) public class CraigsRandomizer { // generates a random alpha-numeric sequence public static void main(String[] args) { CraigsRandomizer t = new CraigsRandomizer(); t.randomPass(12); // set to length of 12 } final void randomPass(int len) { int[] random = new int[len]; int i = 0; String result = ""; while(i random.length){ int n = (int)(Math.random() * 122); if (!(n 49) (!((n = 58) (n = 64))) (!((n = 91) (n = 96 { random[i] = n; i++; } } for(int j = 0; j random.length; j++) { result += (char)random[j]; } random = null; System.out.println(result); } } Let me know if I can help. Regards, Craig O'Brien Java Programmer/Web Developer -Oryginalna wiadomooe- Od: Cato, Christopher [mailto:[EMAIL PROTECTED]] Wysano: 1 marca 2001 15:05 Do: '[EMAIL PROTECTED]' Temat: Generating a random alphanumeric string Hello, can anyone show me an example or give me a clue about how to generate a random alphanumeric string of lets say 16-32 chars? TomCat is obviously doing it for the session ids, but how would I do the same in a servlet? Regards, Christopher Cato - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, email: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, email: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, email: [EMAIL PROTECTED]