Generate A Random String Id

tranni 0 Tallied Votes 166 Views Share

This class Generate A unique string. You can concatenate Timestamp to this Unique String and use it as Unique ID

package test;



/*
* @author NAPSTAR
 */

import java.util.Random;

public class XUIDGenerator {
    private static final int NUM_CHARS = 10;
    private static String    chars     = "abcdefghijklmonpqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    private static Random    r         = new Random();


   public static String getUniqueID() {
        char[] buf = new char[NUM_CHARS];

        for (int i = 0; i < buf.length; i++) {
            buf[i] = chars.charAt(r.nextInt(chars.length()));
        }

        return new String(buf);
    }

    public static void main(String[] args) {
        System.out.println(XUIDGenerator.getUniqueID());
    }
}