You are better off just generating a random long value, then all the bits are random. In Java 6, new Random() uses the System.nanoTime() plus a counter as a seed.
There are different levels of uniqueness.
If you need uniqueness across many machines, you could have a central database table for allocating unique ids, or even batches of unique ids.
If you just need to have uniqueness in one app you can just have a counter (or a counter which starts from the currentTimeMillis()*1000 or nanoTime() depending on your requirements)
I thinks this is the best example for using randomUUID :
Raymond Chen has a really excellent blog post on this:
You are better off just generating a random long value, then all the bits are random. In Java 6, new Random() uses the System.nanoTime() plus a counter as a seed.
There are different levels of uniqueness.
If you need uniqueness across many machines, you could have a central database table for allocating unique ids, or even batches of unique ids.
If you just need to have uniqueness in one app you can just have a counter (or a counter which starts from the currentTimeMillis()*1000 or nanoTime() depending on your requirements)
Raymond Chen has a really excellent blog post on this:
Raymond Chen has a really excellent blog post on this:
Use Time YYYYDDDD
(Year + Day of Year) as prefix. This decreases database fragmentation in tables and indexes. This method returns byte[40]
. I used it in a hybrid environment where the Active Directory SID (varbinary(85)
) is the key for LDAP users and an application auto-generated ID is used for non-LDAP Users. Also the large number of transactions per day in transactional tables (Banking Industry) cannot use standard Int
types for Keys
private static final DecimalFormat timeFormat4 = new DecimalFormat("0000;0000");
public static byte[] getSidWithCalendar() {
Calendar cal = Calendar.getInstance();
String val = String.valueOf(cal.get(Calendar.YEAR));
val += timeFormat4.format(cal.get(Calendar.DAY_OF_YEAR));
val += UUID.randomUUID().toString().replaceAll("-", "");
return val.getBytes();
}
I thinks this is the best example for using randomUUID :
Source: Stackoverflow.com