title: Generate a random number of non duplicated random number in [0, 1001] through a loop
body: <p>I need to generate a random number of non duplicated random number in plpgsql. The non duplicated number shall fall in the range of [1,1001]. However, the code generates number exceeding 1001.</p>
  
  <pre><code>directed2number := trunc(Random()*7+1);
  counter := directed2number
  while counter &gt; 0
  loop
  to_point := trunc((random() * 1/directed2number - counter/directed2number + 1) * 1001 +1);
  ...
  ...
  counter := counter - 1;
  end loop;
  </code></pre>