COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### How to generate vector of random numbers in any interval (e.g. [-1 1]) that sums to 1)

• Email
• Follow

```Hello,
I'd like to generate a vector of random numbers,which take values in any interval (e.g. [-1 1]),and this vector sums to 1...

Vassilis...
```
 1

See related articles to this posting

```Just renormalise:

y = rand(1,N);
S = sum(y);
y = y/S;           % renormalisation
```
 0

```"Vassilios Vassiliadis" <v.vassiliadis@fme.aegean.gr> wrote in message <ibbfkv\$l6m\$1@fred.mathworks.com>...
> Hello,
> I'd like to generate a vector of random numbers,which take values in any interval (e.g. [-1 1]),and this vector sums to 1...
>
> Vassilis...

http://www.mathworks.com/matlabcentral/fileexchange/9700-random-vectors-with-fixed-sum

Bruno
```
 0

```On Nov 9, 7:43=A0am, "Vassilios Vassiliadis"
<v.vassilia...@fme.aegean.gr> wrote:
> Hello,
> I'd like to generate a vector of random numbers,which take values in any =
interval (e.g. [-1 1]),and this vector sums to 1...
>
> Vassilis...

------------------------------------------------------------
I'm not sure that's possible.  Can you tell me how you could take a
bunch of numbers in the interval [-100 -50] and have that sum to
1????  If you made the sum equal to 1, you'd no longer have values in
the range -100 to -50 - you'd have to change the range (the values of
the numbers).  I don't think you can have it both ways: both in the
range and with the prescribed sum.
```
 0

```Firstly, my original post misunderstimated the problem (so apologies for that).

Secondly, are both the interval and the number of random points required fixed at the outset or is it just the interval?  If it's the interval, then N must be allowed to vary so that the constraint that the total equals one can be satisfied (e.g.  elements randomly chosen from I = [-100,1] with lots of points in the positive region).  Otherwise, if both N and the interval are fixed, and the constraint is imposed, then not all of your points are random...

Finally, as mentioned above, your constraint a priori limits your choice of interval.  So, I presume you mean "any interval for N points such that the total of the elements equals 1"?

> ------------------------------------------------------------
> I'm not sure that's possible.  Can you tell me how you could take a
> bunch of numbers in the interval [-100 -50] and have that sum to
> 1????  If you made the sum equal to 1, you'd no longer have values in
> the range -100 to -50 - you'd have to change the range (the values of
> the numbers).  I don't think you can have it both ways: both in the
> range and with the prescribed sum.
```
 0

```For N random number in (a,b), of course it is clear the sum target value must be within (N*a,N*b) so that the event is possible. In that case, Roger's FEX.

Bruno
```
 0

```On 09/11/10 6:43 AM, Vassilios Vassiliadis wrote:

> I'd like to generate a vector of random numbers,which take values in any
> interval (e.g. [-1 1]),and this vector sums to 1...

If you use a fixed number of entries in the vector, then the last one
will never be random: it's value is fully predictable by knowing the
previous ones.

If you do not use a fixed number of entries and just keep generating
until the sum is 1, then your expected wait time for generation would be
*quite* large (i.e., the only thing that keeps it from being infinite is
the fact that binary floating point values have a fixed finite resolution.)
```
 0

```Walter Roberson <roberson@hushmail.com> wrote in message <PEdCo.12435\$lL3.8212@newsfe08.iad>...
> On 09/11/10 6:43 AM, Vassilios Vassiliadis wrote:
>
> > I'd like to generate a vector of random numbers,which take values in any
> > interval (e.g. [-1 1]),and this vector sums to 1...
>
> If you use a fixed number of entries in the vector, then the last one
> will never be random: it's value is fully predictable by knowing the
> previous ones.
> ........
- - - - - - - - -
It does make sense to have random vectors which have a given sum, Walter.  It is a standard problem in conditional probability analogous to asking to simulate a random toss of two dice given that their sum has been observed to be say seven.  If two values, x and y, of the 'rand' function are chosen, this can be viewed statistically as the selection of a random point in a unit square in the x-y plane where equal areas have equal probabilities.  However if we require that their sum be given as say one, then it is a conditional probability problem and can be regarded as a uniformly distributed selection along the line of the square's diagonal where equal lengths then have equal probabilities as required by the conditional probability concept.

Vassilios is asking how to generate random points in a certain polytope in n-dimensional space corresponding to a given sum of random variables, and this makes perfectly good sense.  Unfortunately if one simply normalizes a set of random values from 'rand' so as to have the given sum, this skews the distribution.  In the case of the unit square doing so would have the effect of projecting the square's area orthogonally onto its diagonal which produces a triangular distribution along its length.  It requires a little more sophistication to produce a uniform distribution length-wise, which is why I wrote the 'randfixedsum' routine.

Roger Stafford
```
 0

```"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ibbgev\$emm\$1@fred.mathworks.com>...
> > Hello,
> > I'd like to generate a vector of random numbers,which take values in any interval (e.g. [-1 1]),and this vector sums to 1...
> >
> > Vassilis...
>
> http://www.mathworks.com/matlabcentral/fileexchange/9700-random-vectors-with-fixed-sum
>
> Bruno

Thanx,I find it very useful...
```
 0

```Thank you very much for your help...

Vassilios

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <ibbv7h\$da7\$1@fred.mathworks.com>...
> Walter Roberson <roberson@hushmail.com> wrote in message <PEdCo.12435\$lL3.8212@newsfe08.iad>...
> > On 09/11/10 6:43 AM, Vassilios Vassiliadis wrote:
> >
> > > I'd like to generate a vector of random numbers,which take values in any
> > > interval (e.g. [-1 1]),and this vector sums to 1...
> >
> > If you use a fixed number of entries in the vector, then the last one
> > will never be random: it's value is fully predictable by knowing the
> > previous ones.
> > ........
> - - - - - - - - -
>   It does make sense to have random vectors which have a given sum, Walter.  It is a standard problem in conditional probability analogous to asking to simulate a random toss of two dice given that their sum has been observed to be say seven.  If two values, x and y, of the 'rand' function are chosen, this can be viewed statistically as the selection of a random point in a unit square in the x-y plane where equal areas have equal probabilities.  However if we require that their sum be given as say one, then it is a conditional probability problem and can be regarded as a uniformly distributed selection along the line of the square's diagonal where equal lengths then have equal probabilities as required by the conditional probability concept.
>
>   Vassilios is asking how to generate random points in a certain polytope in n-dimensional space corresponding to a given sum of random variables, and this makes perfectly good sense.  Unfortunately if one simply normalizes a set of random values from 'rand' so as to have the given sum, this skews the distribution.  In the case of the unit square doing so would have the effect of projecting the square's area orthogonally onto its diagonal which produces a triangular distribution along its length.  It requires a little more sophistication to produce a uniform distribution length-wise, which is why I wrote the 'randfixedsum' routine.
>
> Roger Stafford
```
 0

```Hello,
another issue which I've not managed to tackle is the following:
Assuming that I generate a random vector of numbers restricted (only!!!) in the interval
[-1 1].Afterwards,as a second step,I'd like to properly 'transform' (is a sense) this vector in order to sum up to 1.How can I achieve this??

Vassilios...

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <ibbv7h\$da7\$1@fred.mathworks.com>...
> Walter Roberson <roberson@hushmail.com> wrote in message <PEdCo.12435\$lL3.8212@newsfe08.iad>...
> > On 09/11/10 6:43 AM, Vassilios Vassiliadis wrote:
> >
> > > I'd like to generate a vector of random numbers,which take values in any
> > > interval (e.g. [-1 1]),and this vector sums to 1...
> >
> > If you use a fixed number of entries in the vector, then the last one
> > will never be random: it's value is fully predictable by knowing the
> > previous ones.
> > ........
> - - - - - - - - -
>   It does make sense to have random vectors which have a given sum, Walter.  It is a standard problem in conditional probability analogous to asking to simulate a random toss of two dice given that their sum has been observed to be say seven.  If two values, x and y, of the 'rand' function are chosen, this can be viewed statistically as the selection of a random point in a unit square in the x-y plane where equal areas have equal probabilities.  However if we require that their sum be given as say one, then it is a conditional probability problem and can be regarded as a uniformly distributed selection along the line of the square's diagonal where equal lengths then have equal probabilities as required by the conditional probability concept.
>
>   Vassilios is asking how to generate random points in a certain polytope in n-dimensional space corresponding to a given sum of random variables, and this makes perfectly good sense.  Unfortunately if one simply normalizes a set of random values from 'rand' so as to have the given sum, this skews the distribution.  In the case of the unit square doing so would have the effect of projecting the square's area orthogonally onto its diagonal which produces a triangular distribution along its length.  It requires a little more sophistication to produce a uniform distribution length-wise, which is why I wrote the 'randfixedsum' routine.
>
> Roger Stafford
```
 0

```"Vassilios Vassiliadis" <v.vassiliadis@fme.aegean.gr> wrote in message <ibe24n\$9cr\$1@fred.mathworks.com>...
> Hello,
> another issue which I've not managed to tackle is the following:
> Assuming that I generate a random vector of numbers restricted (only!!!) in the interval
> [-1 1].Afterwards,as a second step,I'd like to properly 'transform' (is a sense) this vector in order to sum up to 1.How can I achieve this??
>

A proper way is to describe in term of probability distribution, covariance, conditional probability, then you might get some accurate answer. Because the term "properly transform" does not evoke any meaningful notion to anybody but you.

To me Roger's uniform conditional probability is the most natural. Anything deviates from that is artificial thus must be specified accurately.

Bruno
```
 0