[R] generate random numbers that sum up to 1

Duncan Murdoch murdoch at stats.uwo.ca
Tue Oct 10 17:37:41 CEST 2006


On 10/10/2006 9:53 AM, Peter Dalgaard wrote:
> "sun" <as at hut.at> writes:
> 
>> I am trying to generate a vector of random numbers with the constraint that 
>> they have to sum up to one with uniform distribution.
>> 
>> eg. {0.1,0.7,0.2 }
>> 
>> any function to do this? Thanks.
> 
> Depending on what you mean by "uniform", this may be a solution
> 
>> x<-runif(3)
>> x/sum(x)
> [1] 0.1130642 0.4098608 0.4770750
> 
> What you can't have is the individual components uniform and
> identically distributed because the sum of the means would be 1.5 and
> equal to the mean of the sum which is 1...
> 

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.  RSiteSearch finds at least a couple of packages (VGAM, 
MCMCpack) that provide simulations from the Dirichlet.

Duncan Murdoch



More information about the R-help mailing list