Generating n random variables whose summation will be 1. [I got the answer.]


On genetic algorithm, we have to maintain population. Say, I have two individuals a and b. Every individual consists of $n$ pairs of ($x_i, \theta_i$), where $ 0 \leq i < n$. A fitness function evaluates fitness, $f$ of every individual. Constraint is for every individual is $\Sigma\theta_i \approx 1$ ($0.95 \leq \Sigma\theta_i < 1.05$ would suffice). $\theta_i$ associated with individual a will be adapted by some function (which I haven't figured out yet) of $d(a, b)$ & $\Delta f$. $\theta_i$ will be adaptive (by I guess something like covariance matrix). So if I increase value of $\theta_i$, values of some $\theta_j$ have to be decreased to maintain summation $\Sigma\theta_i \approx 1$. So I am seeking suggestion how can be $\theta_i$ adapted based on $d(a, b)$ & $\Delta f$?

  • 2
    $\begingroup$ Questions: What do you mean by "parameter"? (Do you mean you want to generate $n$ random variates according to your condition?) What do you mean by "error margin"? What other (distributional, etc.) constraints would you like to be satisfied? The answer to your question is undoubtedly yes, but how to go about it will depend other details of your problem. Please edit your question accordingly. $\endgroup$
    – cardinal
    Jun 21, 2011 at 22:23
  • $\begingroup$ FWIW, question is edited. TIA $\endgroup$
    – user
    Jun 21, 2011 at 22:54
  • 10
    $\begingroup$ One answer: generate $n$ random values. Divide each by their total. This works whenever their total is nonzero. The generality of this solution highlights the real question: how do you want those random values to be distributed? $\endgroup$
    – whuber
    Jun 21, 2011 at 23:28
  • $\begingroup$ @whuber: great solution. You have solved another problem of mine. Wish I could vote up you more. $\endgroup$
    – user
    Jun 22, 2011 at 9:54
  • $\begingroup$ @whuber: this is may be silly question. The range of generated value 1<*x*<100 or 1<*x*<10, will it make any difference between these range? $\endgroup$
    – user
    Jun 22, 2011 at 12:28

2 Answers 2


You problem is that of generating pseudo random numbers on the edges of a simplex. The best method is to:

  1. Generate $x_1,...,x_{n^2}\sim \exp(1)$,
  2. Stacke them in a $n\times n$ matrix,
  3. Divide them by their row-wise sum.

I can't find a seminal paper now, but you should look for the algorithms described here.

  • 2
    $\begingroup$ Here is a reference for your answer: "Andrew Gelman, Bayesian Data Analysis, pp.482" as given here: mathworks.com/matlabcentral/newsreader/view_thread/22703 $\endgroup$
    – petrichor
    Jun 22, 2011 at 11:16
  • $\begingroup$ This is just a special case of @Harsh's answer. Also, what do you mean by "the best method"? $\endgroup$
    – cardinal
    Jun 22, 2011 at 22:18

How can I generate n random numbers whose summation will be around 1?

You can try the Dirichlet distribution (see also, https://statipedia.org/wiki/index.php?title=Dirichlet).

You need to specify more details about how they're distributed; using only the sum-constraint condition allows too many possible distributions over parameters. The Dirichlet is a commonly used distribution which satisfies this property.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.