title: Generating unique reference number
body: <p><strong>Tech Stack</strong>: Java 1.6, JPA (Hibernate 3), Spring 3, Oracle 11g</p>
  
  <p>I am working on a project where one of the requirement is to give back the customers a ‘ReferenceNumber’.</p>
  
  <p>One option is to return the row ID, but for that to work, it must not be sequential. Otherwise, you can guess the next number etc.</p>
  
  <p>I can generate a number in Java and store it in a separate column, but then I’ll have make sure there are no collisions.</p>
  
  <p>There are ways to generate such number in database, but not sure if it will guarantee uniqueness.</p>
  
  <p><em>Is there a best practice for such a requirement from the database point of view?</em></p>
  
  <h2><strong>UPDATE 1</strong></h2>
  
  <p>Current I am using the following in Java to generrate the number:</p>
  
  <pre><code>    private static SecureRandom random = new SecureRandom();
  
      public static BigInteger getNew() {
          return new BigInteger(60, random);
      }
  
      public static BigInteger getNew(int numBits) {
          return new BigInteger(numBits, random);
      }
  </code></pre>
  
  <h2><strong>UPDATE 2: Requirement</strong></h2>
  
  <p>Allowing sequential number would allow:</p>
  
  <ol>
  <li>customer to guess the next number.</li>
  <li>Find out how many numbers (orders) were there between two number. etc</li>
  </ol>
  
  <p>It is preferable for this reference to be a number, but say a three letter prefix follwed by number is also fine.</p>