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

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

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 / sum(X); X2 <- X / 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,])
```

hist(X)

"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).

Alberto Monteiro

R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html 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 https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.