[R] Generate random numbers under constrain

Mikhail Umorin mikeumo at gmail.com
Thu Nov 27 22:14:33 CET 2014


How about generating the uniform numbers sequentially and keep the running sum of all the previous numbers. At each step check if the newly generated random number plus the running sum > 1 discard the number and generate a new one, repeat the condition check. If the new number plus old sum < 1 accept the number and repeat the step. Until you got n numbers. 

Mikhail



> On Nov 27, 2014, at 12:20, Erich Neuwirth <erich.neuwirth at univie.ac.at> wrote:
> 
> You want random numbers within the n-dimensional simplex (sum xi <=1)
> The easiest solution of course would be creating n-dimensions vectors
> with iid uniform components on [0,1) and throwing away those
> violating the inequality.
> Since the volume of the n-dimensional simplex is 1/n! (factorial)
> this becomes very wasteful even for low dimensions.
> 
> A possible  answer is to use the Dirichlet distribution from package lca)
> since the uniform distribution on the simplex is a special case
> of the Dirichlet distribution.
> 
> 
>> On Nov 27, 2014, at 14:57, Jue Lin-Ye <jl.iccp at gmail.com> wrote:
>> 
>> Hello! I am relatively new using R, but this is my contribution to the
>> answer. How about using a Monte-Carlo method. Generate m numbers in the
>> support [0,1], where m>n. Then constrain by constructing a loop that takes
>> every one of the elements in the m sized vector and select the ones that
>> sum up to one. If it is very uncommon to sum up to one, given the
>> distribution that you use to generate the random numbers, you can construct
>> a loop that generates as many random numbers as you need and then follow
>> the steps
>> 1.generate
>> 2.select
>> 
>> until you find a total of n number.
>> 
>>> 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
>> 
>> ​Best regards,​
>> 
>> -- 
>> Jue Lin-Ye
>> 
>> ---------------------------------------------------------------
>> Civil Engineering phD candidate
>> Maritime Engineering Laboratory (LIM)
>> Universitat Politècnica de Catalunya (UPC)
>> C/Jordi Girona 1-3, Barcelona 08034 (Spain)
>> -----------------------------------------------------------------
>> 
>>    [[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.
> 
> ______________________________________________
> 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