So this seems like a really simple question to me, but I can't seem to figure it out. I'm using R and I'm trying to generate a random sample, where the generated sample all sums to a set number.
Asked
Active
Viewed 221 times
1 Answers
1
Here's an approach to consider. Generate some random numbers:
n = 10
x <- runif(n) # or rnorm, rpois, whatever you want to use
And then scale()
them to get the sum you want.
tot = 100 # this is the sum you want
x <- scale(x, center=FALSE, scale=sum(x)/tot)
all.equal(sum(x), tot) #TRUE

C8H10N4O2
- 18,312
- 8
- 98
- 134