title: Generate unique random numbers in Postgresql with fixed length
body: <p>I need to generate unique random numbers in Postgresql with a fixed length of 13 digits.
  I've found a similar <a href="https://stackoverflow.com/questions/12761346/pseudo-encrypt-function-in-plpgsql-that-takes-bigint">thread</a> where was used a sequence encrypted using "pseudo_encrypt", but the returned number was not with a fixed length.</p>
  
  <p>So, what i need is: get an encrypted random sequence with a fixed length of 13 digits where the min value is 0000000000001 and a max value is 9999999999999.</p>
  
  <p>Is it possible? If start with the zeros in front is not possible is not a big problem (i think), i can set them programmatically during the reading from the db, but would be great if Postgresql can do it by itself.</p>
  
  <p>-- EDIT --</p>
  
  <p>After have realized some useful things i must change the question in order to explain better what i need:</p>
  
  <p>I need to generate unique random numbers (bigint) in Postgresql with a fixed max length of 13 digits. Actually i'm trying to use <a href="https://stackoverflow.com/questions/12761346/pseudo-encrypt-function-in-plpgsql-that-takes-bigint">pseudo_encrypt</a> function (64 bit), but the returned number obviously is not with a fixed max length of 13, in the 32 bit case the max length is 10 digits (int), and for the 64 bit is 19 (bigint).</p>
  
  <p>So, how to get an encrypted random sequence with a fixed max length of 13 digits, where the min value is 1 and the max value is 9999999999999 ?</p>
  
  <p>Is it possible to modify the 64 bit pseudo_ecrypt function in order to get this result? Or if is not possible, there are other methods to get an unique sequence with this requirements?</p>
  
  <p><strong>Pseudo Encrypt function (64bit)</strong></p>
  
  <pre><code>CREATE OR REPLACE FUNCTION pseudo_encrypt(VALUE bigint) returns bigint   AS $$
  DECLARE
  l1 bigint;
  l2 bigint;
  r1 bigint;
  r2 bigint;
  i int:=0;
  BEGIN
  l1:= (VALUE &gt;&gt; 32) &amp; 4294967295::bigint;
  r1:= VALUE &amp; 4294967295;
  WHILE i &lt; 3 LOOP
      l2 := r1;
      r2 := l1 # ((((1366.0 * r1 + 150889) % 714025) / 714025.0) * 32767*32767)::int;
      l1 := l2;
      r1 := r2;
      i := i + 1;
  END LOOP;
  RETURN ((l1::bigint &lt;&lt; 32) + r1);
  END;
  $$ LANGUAGE plpgsql strict immutable;
  </code></pre>