title: How to get 32 bits unique number in Java?
body: <p>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? </p>
  
  <p>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)</p>
  
  <p>I tried UUID class but it seems the data it generates has more bits than I can use. </p>
  
  <p>I found this but not sure if it works:</p>
  
  <pre><code>    // 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);
  </code></pre>