title: C++ randomly sample k numbers from range 0:n-1 (n > k) without replacement
body: <p>I'm working on porting a MATLAB simulation into C++. To do this, I am trying to replicate MATLAB's <a href="http://www.mathworks.com/help/stats/randsample.html" rel="noreferrer">randsample() function</a>. I haven't figured out an efficient way to do this yet.</p>
  
  <p>So I ask you all, how do I best randomly sample k numbers from a range 0:n-1 (for n > k) without replacement in C++?</p>
  
  <p>I've considered the following pseudocode (inspired by the third example on <a href="http://en.cppreference.com/w/cpp/algorithm/random_shuffle" rel="noreferrer">cppreference.com</a>), but I feel like it's a bit hacky:</p>
  
  <pre><code>initialize vect&lt;int&gt; v of size n
  for i = 0 to n-1
      v[i] = i
  shuffle v
  return v[0 to k-1]
  </code></pre>
  
  <p>The drawback here is also the requirement to build a massive array first too. That seems like slow/clunky overkill.</p>
  
  <p>I would love some direction here if you can help. I'm less interested in the theory (algorithms are interesting but not relevant to my needs now) than the best way to implement this in C++.</p>
  
  <p>Thanks in advance!</p>