Efficient random array of probability weights that

2019-08-04 07:39发布

问题:

I need something very simple and it seems to me that my solution has too many steps (considering that R is done for statistics):

All I need is an array of N probability weights (between 0 and 1) that sum up to one. What would be an easy and/or efficient solution?

My solution:

N <- 40
weights <- runif(40)
f <- 1/sum(weights) 
weights <- f*weights
print(sum(weights))