vxs8122
vxs8122

Reputation: 844

Generating a vector of values which sum to one but the elements should be approximately but NOT exactly uniform

Suppose I want to make a vector of length N. My goal is to ensure that all the elements are approximately 1/N (but not all elements should be exactly 1/N) and the sum of the vector should equal to one. Is there a way to do this in R?

Upvotes: 0

Views: 42

Answers (1)

Stéphane Laurent
Stéphane Laurent

Reputation: 84519

You can use a Dirichlet distribution, with large and equal parameters.

> library(bayesm)
> rdirichlet(c(100,100,100))
          [,1]
[1,] 0.3360327
[2,] 0.3280179
[3,] 0.3359495
> rdirichlet(c(10000,10000,10000))
          [,1]
[1,] 0.3311760
[2,] 0.3353209
[3,] 0.3335032
> rdirichlet(c(1000000,1000000,1000000))
          [,1]
[1,] 0.3330997
[2,] 0.3334884
[3,] 0.3334119

Upvotes: 4

Related Questions