# Random number generation

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

EDIT

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$?

• 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. – cardinal Jun 21 '11 at 22:23
• FWIW, question is edited. TIA – user Jun 21 '11 at 22:54
• 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? – whuber Jun 21 '11 at 23:28
• @whuber: great solution. You have solved another problem of mine. Wish I could vote up you more. – user Jun 22 '11 at 9:54
• @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? – user Jun 22 '11 at 12:28

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.

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

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