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]

Reply via email to