Question

I need to generate an unique number of 32 bits in Java. I need to return the number as Java int, which is required by the interface. Can you please share some ideas on this?

The number will be used as MySQL PK and several threads could be generating their own unique id at same time. (Sorry it is decided not to use MySQL incremental id)

I tried UUID class but it seems the data it generates has more bits than I can use.

I found this but not sure if it works:

    // seems no way to get int
    UUID id = UUID.randomUUID();
    System.out.println(id);

    // not working either?
    java.rmi.server.UID uid = new java.rmi.server.UID();
    System.out.println(uid.toString());

    // the one i am using
    SecureRandom prng = SecureRandom.getInstance("SHA1PRNG");
    prng.setSeed(System.currentTimeMillis());
    int ret = prng.nextInt();
    System.out.println(ret);
Was it helpful?

Solution

How "unique" are you wanting? In short, what is the collision domain? If you are dealing with thousands of keys then Random.nextInt() does exactly what you want relative to what you tried with version 4 UUIDs (UUID v4 generates 128 random bits).

If you need something with a less chance of collision then you need to have a globally incremented integer but there is much care to be taken here such as keeping state between JVM startups. For that you should look into AtomicIntegers.

OTHER TIPS

The SecureRandom approach is fine, but don't set the seed on it. It will choose it's own seed in a (presumably) secure manner.

You could also use a UUID and just discard the bits you don't need, e.g.

int key = (int)UUID.randomUUID().getLeastSignificantBits();

EDIT: You should also be aware that the SecureRandom is significantly slower than Random. Since you're not doing crypto here, why not use Random?

I think you can use a 32-bit hash function. details are given in the following tutorial http://www.concentric.net/~ttwang/tech/inthash.htm

private static int hash(int key){

          key = ~key + (key << 15); // key = (key << 15) - key - 1;
          key = key ^ (key >>> 12);
          key = key + (key << 2);
          key = key ^ (key >>> 4);
          key = key * 2057; // key = (key + (key << 3)) + (key << 11);
          key = key ^ (key >>> 16);
          return key;

    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top