Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

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

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

From: Vassilios Vassiliadis

Date: 9 Nov, 2010 12:43:11

Message: 1 of 12

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...

Thanx in advance...
Vassilis...

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

From: PLH

Date: 9 Nov, 2010 12:56:06

Message: 2 of 12

Just renormalise:

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

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

From: Bruno Luong

Date: 9 Nov, 2010 12:57:03

Message: 3 of 12

"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...
>
> Thanx in advance...
> Vassilis...

Roger's fex submission is taylor made for your problem:
http://www.mathworks.com/matlabcentral/fileexchange/9700-random-vectors-with-fixed-sum

Bruno

Subject: How to generate vector of random numbers in any interval (e.g.

From: ImageAnalyst

Date: 9 Nov, 2010 13:06:27

Message: 4 of 12

On Nov 9, 7:43 am, "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...
>
> Thanx in advance...
> 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.

Subject: How to generate vector of random numbers in any interval (e.g.

From: PLH

Date: 9 Nov, 2010 13:26:05

Message: 5 of 12

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.

Subject: How to generate vector of random numbers in any interval (e.g.

From: Bruno Luong

Date: 9 Nov, 2010 13:46:04

Message: 6 of 12

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

Subject: How to generate vector of random numbers in any interval (e.g.

From: Walter Roberson

Date: 9 Nov, 2010 15:20:14

Message: 7 of 12

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.)

Subject: How to generate vector of random numbers in any interval (e.g.

From: Roger Stafford

Date: 9 Nov, 2010 17:09:05

Message: 8 of 12

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

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

From: Vassilios Vassiliadis

Date: 10 Nov, 2010 09:09:03

Message: 9 of 12

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ibbgev$emm$1@fred.mathworks.com>...
> "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...
> >
> > Thanx in advance...
> > Vassilis...
>
> Roger's fex submission is taylor made for your problem:
> http://www.mathworks.com/matlabcentral/fileexchange/9700-random-vectors-with-fixed-sum
>
> Bruno

Thanx,I find it very useful...

Subject: How to generate vector of random numbers in any interval (e.g.

From: Vassilios Vassiliadis

Date: 10 Nov, 2010 09:15:04

Message: 10 of 12

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

Subject: How to generate vector of random numbers in any interval (e.g.

From: Vassilios Vassiliadis

Date: 10 Nov, 2010 12:11:03

Message: 11 of 12

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??

Thanx in advance...
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

Subject: How to generate vector of random numbers in any interval (e.g.

From: Bruno Luong

Date: 10 Nov, 2010 12:37:04

Message: 12 of 12

"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

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us