Sample uniformly at random from an n-dimensional unit simplex

前端 未结 5 1907
礼貌的吻别
礼貌的吻别 2020-12-14 02:36

Sampling uniformly at random from an n-dimensional unit simplex is the fancy way to say that you want n random numbers such that

  • they are all non-negative,
5条回答
  •  心在旅途
    2020-12-14 03:00

    Here's a nice concise implementation of the second algorithm from Wikipedia:

    SimplexSample[n_] := Rest@# - Most@# &[Sort@Join[{0,1}, RandomReal[{0,1}, n-1]]]
    

    That's adapted from here: http://www.mofeel.net/1164-comp-soft-sys-math-mathematica/14968.aspx (Originally it had Union instead of Sort@Join -- the latter is slightly faster.)

    (See comments for some evidence that this is correct!)

提交回复
热议问题