[R] Generate random numbers under constrain

Boris Steipe boris.steipe at utoronto.ca
Sat Nov 22 16:29:18 CET 2014


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:

> 
> Dear all,
> I use R 3.1.1 for Windows.
> kindly how can I generate n number of random numbers with probability from [0,1]
> and their sum must not be more than one
> thanks in advance
> Ragia
> 
> 
> 		 	   		  
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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