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

From: Grant Izmirlian <>
Date: Wed 11 Oct 2006 - 20:23:52 GMT

Duncan Murdoch's definition is _the_ only one that I know. X is Uniform on A means E phi(X) = \int_A phi(x) dx / \int_A dx, so that the probability density is equal to 1/ \int_A dx everwhere on the set A.

By the way, another way to simulate X ~ Dirichlet(A1, A2, ..., Ad) is to generate d independent gamma variables having equal rate parameter (doesn't matter, so why not 1) and shape parameters A1, A2, ..., Ad Then the vector of components divided by their sum is the desired Dirichlet:

n <- 100000
d <- 3  # for three numbers that add to one ( the unit simplex in R^3)
A <- rep(1, 3)  # for uniform
X <- matrix(0, n, d)

for (k in 1:3) X[,k] <- rgamma(n, shape=A[k], rate=1) S <- X %*% rep(1, d)
Y <- X/S

Present example will simulate n independant 3 vectors, each having non-negative components summing to 1, and having a distribution assigning equal mass to every possible value.

Changing d and the components of A will provide an arbitrary Dirichlet on the unit simplex in R^d

Grant Izmirlian

>> Duncan Murdoch wrote "Another definition of uniform is to have equal
>> density for all possible vectors; the Dirichlet distribution with
>> parameters (1,1,1) would give you that. " mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Thu Oct 12 06:32:27 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 - 21:30:09 GMT.

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