title: Sample uniformly at random from an n-dimensional unit simplex
body: <p>Sampling uniformly at random from an n-dimensional unit simplex is the fancy way to say that you want n random numbers such that</p>
  
  <ul>
  <li>they are all non-negative,</li>
  <li>they sum to one, and</li>
  <li>every possible vector of n non-negative numbers that sum to one are equally likely.</li>
  </ul>
  
  <p>In the n=2 case you want to sample uniformly from the segment of the line x+y=1 (ie, y=1-x) that is in the positive quadrant.
  In the n=3 case you're sampling from the triangle-shaped part of the plane x+y+z=1 that is in the positive octant of R3:</p>
  
  <p><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/3/38/2D-simplex.svg/200px-2D-simplex.svg.png"/></p>
  
  <p>(Image from <a href="http://en.wikipedia.org/wiki/Simplex" rel="noreferrer">http://en.wikipedia.org/wiki/Simplex</a>.)</p>
  
  <p>Note that picking n uniform random numbers and then normalizing them so they sum to one does not work.  You end up with a bias towards less extreme numbers.</p>
  
  <p>Similarly, picking n-1 uniform random numbers and then taking the nth to be one minus the sum of them also introduces bias.</p>
  
  <p>Wikipedia gives two algorithms to do this correctly:  <a href="http://en.wikipedia.org/wiki/Simplex#Random_sampling" rel="noreferrer">http://en.wikipedia.org/wiki/Simplex#Random_sampling</a>
  (Though the second one currently claims to only be correct in practice, not in theory. I'm hoping to clean that up or clarify it when I understand this better. I initially stuck in a "WARNING: such-and-such paper claims the following is wrong" on that Wikipedia page and someone else turned it into the "works only in practice" caveat.)</p>
  
  <p>Finally, the question:
  What do you consider the best implementation of simplex sampling in Mathematica (preferably with empirical confirmation that it's correct)?</p>
  
  <p>Related questions</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/2171074/generating-a-probability-distribution">Generating a probability distribution</a></li>
  <li><a href="https://stackoverflow.com/questions/3007975/java-random-percentages">java random percentages</a></li>
  </ul>