[R] How to draw 4 random weights that sum up to 1?

Alexander Engelhardt alex at chaotic-neutral.de
Mon Oct 10 18:10:38 CEST 2011


Hey list,

This might be a more general question and not that R-specific. Sorry for 
that.

I'm trying to draw a random vector of 4 numbers that sum up to 1.
My first approach was something like

   a <- runif(1)
   b <- runif(1, max=1-a)
   c <- runif(1, max=1-a-b)
   d <- 1-a-b-c

but this kind of distorts the results, right?
Would the following be a good approach?

   w <- sample(1:100, 4, replace=TRUE)
   w <- w/sum(w)

I'd prefer a general algorithm-kind of answer to a specific R function 
(if there is any). Although a function name would help too, if I can 
sourcedive.

Thanks in advance,
  Alex



More information about the R-help mailing list