Generate Random UID String with Particular Format in java

Posted by Somesh Shinde On Thursday, 18 May 2017 0 comments
Generate Random UID String with Particular Format


public class UniqueIdUtility {
public static String getSaltString() {
        String SALTCHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        StringBuilder salt = new StringBuilder();
        Random rnd = new Random();
        while (salt.length() < 6) { // length of the random string.
            int index = (int) (rnd.nextFloat() * SALTCHARS.length());
            salt.append(SALTCHARS.charAt(index));
        }
        String saltStr = "#"+salt.toString();
        return saltStr;

    }
}

/* Above is the sample method to generate 6 digit Unique String in java */

0 comments:

Post a Comment