# [R] Generate random numbers under constrain

David Winsemius dwinsemius at comcast.net
Sat Nov 22 20:06:00 CET 2014

```In the 2 and 3 vector case it is possible do define a fairly simple sampling space where this is possible.

Consider the unit square where the sample space is the area where x+y <1.

It generalizes to 3 dimensions with no difficulty.

x= (0:100)/100
y= (0:100)/100
z=outer(x,y, function(x,y) 1-x-y)
library(lattice)
wireframe(z~x+y, data.frame(x=x,y=rep(y,each=101), z) ,zlim=c(0,1) , scales=list(arrows=FALSE))

So I think the OP _is_ asking for a _random_ variable drawn from a sample space in an n-dimensional hyper-"triangular pyramid",  with base being the  n-1 dimensional analogue of an equilateral regular triaggle and the height of the pyramid being  some value that corresponds to a value of

1-(nthroot(of some sum( that I cannot state with clarity right now))

--
David.

On Nov 22, 2014, at 8:01 AM, Boris Steipe wrote:

> Of course they are random. But they can't all be randomly picked from [0,1).
> By scaling them, one is effectively scaling the interval from which they are picked.
>
> B.
> Nb: the scaling procedure will work for any probability distribution.
>
>
>
> On Nov 22, 2014, at 10:54 AM, Ranjan Maitra <maitra.mbox.ignored at inbox.com> wrote:
>
>> I don't understand this discussion at all.
>>
>> n random numbers constrained to have sum <=1 are still random. They are not all independent.
>>
>> That said, the original poster's question is ill=formed since there can be multiple distributions these random numbers come from.
>>
>> best wishes,
>> Ranjan
>>
>>
>>
>> On Sat, 22 Nov 2014 10:29:18 -0500 Boris Steipe <boris.steipe at utoronto.ca> wrote:
>>
>>> 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
>>>> Ragia
>>>>
>>>>
>>>>
>>>> 	[[alternative HTML version deleted]]
>>>>
>>>> ______________________________________________
>>>> R-help at r-project.org mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>>
>> --
>> Important Notice: This mailbox is ignored: e-mails are set to be deleted on receipt. Please respond to the mailing list if appropriate. For those needing to send personal or professional e-mail, please use appropriate addresses.
>>
>> ____________________________________________________________
>> FREE ONLINE PHOTOSHARING - Share your photos online with your friends and family!
>> Visit http://www.inbox.com/photosharing to find out more!
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help