tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Samson, Lyndon [IT]" <lyndon.sam...@ssmb.com>
Subject RE: Generating a random alphanumeric string
Date Thu, 01 Mar 2001 14:40:12 GMT
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:ccato@rational.com]
Sent: Thursday, March 01, 2001 2:05 PM
To: 'tomcat-user@jakarta.apache.org'
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: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, email: tomcat-user-help@jakarta.apache.org

Mime
View raw message