title: Random default column value that rerolls on conflict
body: <p>I have a column that I would like to <code>default</code> to a randomly generated <code>int8</code> in a specified range. I would also like this column to be unique, so if a random value is generated that already exists, it should be rerolled.</p>
  
  <p>So my question is what the most idiomatic way to do the above is in PostgreSQL, ideally with good performance and supporting bulk inserts.</p>
  
  <p>For example if I had a <code>Person</code> table with columns <code>name</code> and <code>id</code>, and I want <code>id</code> to be a random unique <code>int8</code> in the range <code>(0, 999999)</code>. I would want to be able to insert <code>Paul</code>, <code>Kelly</code>, <code>David</code> and <code>Katie</code> and get something like the following:</p>
  
  <pre><code>| Name  |   id   |
  +-------+--------+
  | Paul  | 314563 |
  | Kelly | 592103 |
  | David | 127318 |
  | Katie | 893134 |
  </code></pre>
  
  <p>With no risk of duplicates and no risk of an insertion failure.</p>
  
  <p>The range is not going to be large enough for me to safely assume they will never collide (i.e Birthday Paradox).</p>
  
  <p>I should also say I do want true unpredictable randomness, so a cipher on a sequence would not count.</p>
  
  <p>There are a variety of answers on how to generate random numbers, so the main focus of the question is the uniqueness aspect.</p>
  
  <p>With that said a clean and efficient way to generate an <code>int8</code> uniformly in an arbitrarily large range would be appreciated. <code>random() * n</code> starts having gaps when <code>n &gt; 2 ^ 53</code> (perhaps earlier).</p>