Universally unique 64 bit long generator I'm trying to make an ID generator that generates 64 bit longs. The IDs should be globally unique even between two offline machines. This is the same idea as UUID (GUID), but UUID is 128 bit long and contains redundant (constant) bits (eg. it uses the millisecs passed since the year 1980 and has 5-8 versioning bits). I could use either the least or most significant 64 bits of a UUID, but because it contains these constant bits, it may not be unique enough.

So I made my own logic. It's important that it has to run everywhere, so I can't use MAC address, because it's not always accessible (eg. on Android). What do you think of my code?

public class UID {

private static long swRndVal;
private static Random random = new Random();
private static final long mstsigMask = 0b11111111_00000000_00000000_00000000_00000000_00000000_00000000_00000000L;
private static final long middleMask = 0b00000000_11111111_11111111_11111111_11111111_11111111_11111111_11111111L;
private static final long lstsigMask = 0b00000000_00000000_00000000_00000000_00000000_00001111_11111111_11111111L;

static {
    String s1 = System.getProperty("os.arch", "");
    String s2 = System.getProperty("os.version", "");
    String s3 = System.getProperty("java.vm.version", "");
    String s4 = System.getProperty("java.class.version", "");
    Random rand1 = new Random((s1+s2+s3+s4).hashCode());
    Random rand2 = new Random();
    swRndVal = rand1.nextLong() ^ rand2.nextLong();
}

public static long getUniqueLong() {
    //8 bits of random, 36 bits of millis (~800 days), 20 bits of nanos (~1ms)
    return ((random.nextLong() &amp; mstsigMask) | (System.currentTimeMillis() &amp; middleMask) | (System.nanoTime() &amp; lstsigMask)) ^ swRndVal;
}
}
I would just use SecureRandom#nextLong(). SecureRandom will seed itself and shouldn't be the same between two similar computers at the same time. Also, it uses more bits of state than Random (128 vs 48).

I don't know exactly what computers you'll be running this on, but if it's something like a cluster of servers they'll probably have the exact same s1/s2/s3/s4. If they happen to start at the same time (plausible if two processes on two cores are launched by a parents at the same time), then that only leaves the 48 bits of random state.

If you use SecureRandom, you can just call it and immediately get a 64 bit UUID that will have as few collisions as you can possibly guarantee with two offline computers.