Re: [R] generate random numbers that sum up to 1

From: Alberto Monteiro <>
Date: Wed 11 Oct 2006 - 21:04:08 GMT

I don't have the previous messages, but it seems to me that the solutions didn't quite get the requirements of the problem.

For example, "it's obvious that" for n = 2, the random variables X1 and X2 should be X1 <- runif(1) and X2 <- 1 - X1; while the solution X <- runif(2); X1 <- X[1] / sum(X); X2 <- X[2] / sum(X) will give different distributions, as shown in this test case:

N <- 1000
M <- matrix(runif(2 * N), 2, N)
X <- M[1,] / (M[1,] + M[2,])


"It's obvious that" for a generic n-th dimensional set of uniform variables X1, ... X_n subject to the restriction X1 + ... + X_n = 1, the solution is to take a uniform distribution in the (n-1)-th dimensional hyperpyramid generated by the above relation and the restrictions that each X_i >= 0.

For example, for n = 3, we should sample from the equilateral triangle with vertices c(1,0,0), c(0,1,0) and c(0,0,1).

For n = 4, we should sample from the pyramid whose vertices are c(1,0,0,0), c(0,1,0,0), c(0,0,1,0) and c(0,0,0,1).

I don't know if there is a simple formula to do this sampling.

Alberto Monteiro mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Thu Oct 12 07:22:46 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 11 Oct 2006 - 22:30:10 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.