Skip to content
Prev 273987 / 398506 Next

How to draw 4 random weights that sum up to 1?

As an interesting extension to David's post, try:

M4.e <- matrix(rexp(40000,1), ncol=4)

Instead of the uniform and rerun the rest of the code (note the limits on the x-axis).

With 3 dimensions and the restriction we can plot in 2 dimensions to compare:

library(TeachingDemos)

m3.unif <- matrix(runif(3000),  ncol=3)
m3.unif <- m3.unif/rowSums(m3.unif)

m3.exp  <- matrix(rexp(3000,1), ncol=3)
m3.exp  <- m3.exp/rowSums(m3.exp)


dev.new()
triplot(m3.unif)

dev.new()
triplot(m3.exp)

now compare the 2 plots on the density of the points near the corners.