title: Stata - How to Generate Random Integers
body: <p>I am learning Stata and want to know how to generate random integers (without replacement).  If I had 10 total rows, I would want each row to have a unique integer from 1 to 10 assigned to it.  In R, one could simply do:</p>
  
  <pre><code>sample(1:10, 10)
  </code></pre>
  
  <p>But it seems more difficult to do in Stata.  From <a href="http://blog.stata.com/tag/random-numbers/" rel="nofollow">this Stata page</a>, I saw:</p>
  
  <pre><code>generate ui = floor((b-a+1)*runiform() + a)
  </code></pre>
  
  <p>If I substitute a=1 and b=10, I get something close to what I want, but it samples with replacement.</p>
  
  <p>After getting that part figured out, how would I handle the following wrinkle: my data come in pairs.  For example, in the 10 observations, there are 5 groups of 2.  Each group of 2 has a unique identifier.  How would I arrange the groups (and not the observations) in random order?  The data would look something like this:</p>
  
  <pre><code>obs   group  mem     value
  1     A      x       9345
  2     A      y       129
  3     B      x       251
  4     B      y       373
  5     C      x       788
  6     C      y       631
  7     D      x       239
  8     D      y       481
  9     E      x       224
  10    E      y       585  
  </code></pre>
  
  <p><code>obs</code> is the observation number.  <code>group</code> is the group the observation (row) belongs to.  <code>mem</code> is the member identifier in the group.  Each group has one <code>x</code> and one <code>y</code> in it.</p>