Skip to content
Back to formatted view

Raw Message

Message-ID: <4E9318FE.1090502@chaotic-neutral.de>
Date: 2011-10-10T16:10:38Z
From: Alexander Engelhardt
Subject: How to draw 4 random weights that sum up to 1?

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