[R] how to generate a set of random numbers that sum to 1 with uniform distribution of elements

Heramb heramb.gadgil at gmail.com
Wed Nov 7 19:56:12 CET 2012


Hi,

This is a humble try. 

Matrix=function(n){Tab=cbind(runif(n,0,1))
                  for(i in 2:n)
                   {x=NULL
                    for(j in 1:n)
                     {x=c(x,runif(1,0,1-sum(Tab[j,])))}
                    Tab=cbind(Tab,x)
                    }
                  Table<<-Tab
            }

n=1,2,3,4.....
Matrix(n)

1. The columns will follow uniform distribution with respective parameters.
2. The row sums will be approximately equal to '1'.
3. The discrepancy in 2nd Point can be removed if the number of columns are increased.

I hope this works

Best,
Heramb M. Gadgil


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Bärbel
Sent: Wednesday, November 07, 2012 6:32 PM 
To: r-help at r-project.org
Subject: [R] how to generate a set of random numbers that sum to 1 with uniform distribution of elements

Hi,
I am looking for a way to generate a matrix of random numbers in a way that
each row of the matrix would sum to 1 and that the numbers in the columns of
the matrix would have a uniform distribution. So far I have found several
ways to create random numbers that would sum to 1, but then the distribution
of the individual elements is more or less skewed - there are much more
small numbers than larger ones. 
Any help and suggestions?
- Bärbel



--
View this message in context: http://r.789695.n4.nabble.com/how-to-generate-a-set-of-random-numbers-that-sum-to-1-with-uniform-distribution-of-elements-tp4648695.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list