You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Herchel Wojciech <W....@pzuzycie.com.pl> on 2001/03/01 15:02:38 UTC

ODP: Generating a random alphanumeric string

see tomcat sources, org.apache.tomcat.util.SessionIdGenerator namely

vVolf


> -----Oryginalna wiadomooeæ-----
> Od: Cato, Christopher [mailto:ccato@rational.com]
> Wys³ano: 1 marca 2001 15:05
> Do: 'tomcat-user@jakarta.apache.org'
> 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: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, email: tomcat-user-help@jakarta.apache.org
> 

RE: Generating a random alphanumeric string

Posted by Craig O'Brien <cr...@thebridgeproductions.com>.
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:ccato@rational.com]
> Wys³ano: 1 marca 2001 15:05
> Do: 'tomcat-user@jakarta.apache.org'
> 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: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, email: tomcat-user-help@jakarta.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, email: tomcat-user-help@jakarta.apache.org