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:
Generate random numbers with fixed sum and one value constraint

Subject: Generate random numbers with fixed sum and one value constraint

From: Dan

Date: 2 Apr, 2012 19:55:11

Message: 1 of 8

Hi,
how I could generate random number (I need every time 5 to 15 values) :
1) with sum = 1,
2) one of this numbers is higher than 0.6?

First problem is often solved with dividing by sum of all generated numbers, but then the second requirement can't be satisfied. Could you give me some simple solution?

Thanks
Dan

Subject: Generate random numbers with fixed sum and one value constraint

From: Greg Heath

Date: 2 Apr, 2012 20:17:16

Message: 2 of 8

On Apr 2, 3:55 pm, "Dan " <scislowski.dan...@gmail.com> wrote:
> Hi,
> how I could generate random number (I need every time 5 to 15 values) :
> 1) with sum = 1,
> 2) one of this numbers is higher than 0.6?
>
> First problem is often solved with dividing by sum of all generated numbers, but then the second requirement can't be satisfied. Could you give me some simple solution?
>
> Thanks
> Dan

x1 = 0.6+0.4*rand;
x2n0 = rand(1,n-1);
sumx2n0 = sum(x2n0)
x = [ x1, (1-x1)*x2n0/sumx2n0];

Hope this helps.

Greg

Subject: Generate random numbers with fixed sum and one value constraint

From: Dan

Date: 2 Apr, 2012 21:36:12

Message: 3 of 8

Exactly what I need. Thank you.

Greg Heath <g.heath@verizon.net> wrote in message <ab4a0836-6a10-4661-9730-22a513bf4c97@z38g2000vbu.googlegroups.com>...
> On Apr 2, 3:55 pm, "Dan " wrote:
> > Hi,
> > how I could generate random number (I need every time 5 to 15 values) :
> > 1) with sum = 1,
> > 2) one of this numbers is higher than 0.6?
> >
> > First problem is often solved with dividing by sum of all generated numbers, but then the second requirement can't be satisfied. Could you give me some simple solution?
> >
> > Thanks
> > Dan
>
> x1 = 0.6+0.4*rand;
> x2n0 = rand(1,n-1);
> sumx2n0 = sum(x2n0)
> x = [ x1, (1-x1)*x2n0/sumx2n0];
>
> Hope this helps.
>
> Greg

Subject: Generate random numbers with fixed sum and one value constraint

From: ImageAnalyst

Date: 2 Apr, 2012 21:52:29

Message: 4 of 8

On Apr 2, 5:36 pm, "Dan " <scislowski.dan...@gmail.com> wrote:
> Exactly what I need. Thank you.
------------------------------------------------------------
Dan, if you want to make sure that the >0.6 value does not always
occur at the first element, then add this code to Greg's code:

randomIndices = randperm(length(x))
x = x(randomIndices)

That will locate the >0.6 number at a random location.

Subject: Generate random numbers with fixed sum and one value constraint

From: Dan

Date: 2 Apr, 2012 22:05:12

Message: 5 of 8

Actually I need that >0.6 value as first element, but thanks for your interest :)
ImageAnalyst <imageanalyst@mailinator.com> wrote in message <a3741aa4-8fea-467a-9781-cd5e41dacad8@z5g2000yqj.googlegroups.com>...
> On Apr 2, 5:36 pm, "Dan " <scislowski.dan...@gmail.com> wrote:
> > Exactly what I need. Thank you.
> ------------------------------------------------------------
> Dan, if you want to make sure that the >0.6 value does not always
> occur at the first element, then add this code to Greg's code:
>
> randomIndices = randperm(length(x))
> x = x(randomIndices)
>
> That will locate the >0.6 number at a random location.

Subject: Generate random numbers with fixed sum and one value constraint

From: Roger Stafford

Date: 3 Apr, 2012 03:38:11

Message: 6 of 8

"Dan" wrote in message <jld06v$ol0$1@newscl01ah.mathworks.com>...
> Hi,
> how I could generate random number (I need every time 5 to 15 values) :
> 1) with sum = 1,
> 2) one of this numbers is higher than 0.6?
>
> First problem is often solved with dividing by sum of all generated numbers, but then the second requirement can't be satisfied. Could you give me some simple solution?
>
> Thanks
> Dan
- - - - - - - - - -
  If it is given that the first number is the one that must be greater than 0.6, and if all the numbers are non-negative, then this problem is the equivalent of restricting all numbers to the range [0,0.4] with sum 0.4, but then afterwards adding 0.6 to the first number. However if we generate this by using 'rand' independently on each number, then dividing by their sum and multiplying by 0.4, and finally adding 0.6 to the first number, the resultant distribution is distinctly non-uniform over the hyperspace of all possible such sets of numbers. In the simple case of two non-negative numbers whose sum must be 1, you can easily demonstrate this for large n by observing the distribution of values for

 x1 = rand(n,1);
 x2 = rand(n,1);
 x1 = x1./(x1+x2);
 x2 = x2./(x1+x2);
 s = (x2-x1)/sqrt(2); % The metric of distance along the cross diagonal

The quantity s will be seen to have a tent-shaped distribution centered at zero and dropping to zero density at each endpoint. For larger numbers of elements this approaches that of a multivariate gaussian distribution and becomes increasingly non-uniform.

  If you want a uniform distribution, you can make use of a routine I placed in the File Exchange at

 http://www.mathworks.com/matlabcentral/fileexchange/9700

called 'randfixedsum' which will give a uniform distribution. Just do:

 x = randfixedsum(m,n,.4,0,.4);
 x(1,:) = x(1,:)+.6; % Now all column sums will be 1.0

It gives you a matrix of n columns and the sum of the m elements in each column is one. You would thus use m in the range of m = 5 to 15 for your purposes.

Roger Stafford

Subject: Generate random numbers with fixed sum and one value constraint

From: Dmitrey Yershov

Date: 14 Nov, 2012 13:59:11

Message: 7 of 8

Hello! I want to generate random numbers with fixed sum, BUT my problem has such a feature that ai<=xi<=bi (bounds varies from one rundom number to another). Are there some opinions how can I apply the alghorithm of Mr. Stafford - http://www.mathworks.com/matlabcentral/fileexchange/9700 - (may be with some modifications) to solve my problem?

Subject: Generate random numbers with fixed sum and one value constraint

From: Dmitrey Yershov

Date: 14 Nov, 2012 14:00:17

Message: 8 of 8

Hello! I want to generate random numbers with fixed sum, BUT my problem has such a feature that ai<=xi<=bi (bounds varies from one rundom number to another). Are there some opinions how can I apply the alghorithm of Mr. Stafford - http://www.mathworks.com/matlabcentral/fileexchange/9700 - (may be with some modifications) to solve my problem?

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