Monday, January 21, 2013

Java code to generate unique random key

import java.util.Random;
public class RandomKeyGenerator {
      public static String generateRandomKey() {
String allChars = “abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@$%^*()_”;
        Random random = new Random();
        int length = random.nextInt(5);
        length+=7;
        char[] chars = new char[length];
        for (int i=0; i            chars[i] = allChars.charAt(random.nextInt(allChars.length()));
          }
        return new String(chars);
    }
}

No comments:

Post a Comment