Google+ Sandeepa On Life: How to generate a random string in java

Pages

Search this blog

Tuesday, May 31, 2011

How to generate a random string in java

Want to generate a random alpha-numeric string in Java? Here's how you can do it.
If you are into coding, sooner or later you will run into this!

There are many ways to do it. Here's the one I used recently in my code.

import java.security.SecureRandom;
import java.math.BigInteger;

SecureRandom srandom = new SecureRandom();
String rand = new BigInteger(176, srandom).toString(32);
/* and you have your random string in 'rand' */

What random libraries/functions do you use?
Write in comments.

2 comments:

  1. very nice. for more java examples, visit http://java2novice.com site.

    ReplyDelete
  2. Very Useful. For more examples visit http://answersz.com

    ReplyDelete