Skip to content
Prev 345910 / 398513 Next

Generate random numbers under constrain

These are contradictory requirements: either you have n random numbers from the interval [0,1), then you can't guarantee anything about their sum except that it will be in [0,n). Or you constrain the sum, then your random numbers cannot be random in [0,1). You could possibly scale the random numbers:
n <- 13
x <- runif(n)
x <- x / sum(x)
x; sum(x)

This will guarantee that their sum is 1 (to numerical accuracy), but your numbers are then effectively drawn from the interval [0,2/n) for large n.

B.
On Nov 22, 2014, at 9:29 AM, Ragia Ibrahim <ragia11 at hotmail.com> wrote: