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:
Random Vectors with Fixed Sum / Naturals

Subject: Random Vectors with Fixed Sum / Naturals

From: Pablo NB

Date: 11 Apr, 2012 04:50:22

Message: 1 of 9

Hi, I'm looking for a solution for my problem, I'm trying to generate a random vector of nine elements with fixed sum of 3, but they have to be naturals, from 0 - 3. I tried to modify the following file with no success.

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

Thanks in advance for your help.

Subject: Random Vectors with Fixed Sum / Naturals

From: Bruno Luong

Date: 11 Apr, 2012 06:23:12

Message: 2 of 9

"Pablo NB" wrote in message <jm32ie$8me$1@newscl01ah.mathworks.com>...
> Hi, I'm looking for a solution for my problem, I'm trying to generate a random vector of nine elements with fixed sum of 3, but they have to be naturals, from 0 - 3.

diff([0 sort(ceil(3*rand(1,8))) 3])

% Bruno

Subject: Random Vectors with Fixed Sum / Naturals

From: Bruno Luong

Date: 11 Apr, 2012 06:54:22

Message: 3 of 9

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message
>
> diff([0 sort(ceil(3*rand(1,8))) 3])
>

Sorry, the basic random with CEIL is not good. This is correct.

diff([0 sort(floor(4*rand(1,8))) 3])

% Bruno

Subject: Random Vectors with Fixed Sum / Naturals

From: Roger Stafford

Date: 12 Apr, 2012 04:46:27

Message: 4 of 9

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jm39qu$2mj$1@newscl01ah.mathworks.com>...
> diff([0 sort(floor(4*rand(1,8))) 3])
- - - - - - - - - -
  Using diff/sort in that manner does have a statistical shortcoming in my opinion, Bruno. One way to illustrate this is the following. The probabilities of each of the 3^8 = 6561 possible results of

 floor(4*rand(1,8))

are equal. Out of these, just one of them is 3 3 3 3 3 3 3 3, but seventy of them give four 3's and four 0's. After the 'sort' followed by 'diff' the final results of these two cases are

 3 0 0 0 0 0 0 0 0

and

 0 0 0 0 3 0 0 0 0

respectively, and this is the only way to produce these two final results. Thus, the second of these would be seventy times more likely than the first with this method of generation.

  Out of 3^9 = 19683 possible sequences of nine integers from 0 to 3, there are only 165 that yield a sum of 3, nine of them with a single 3, seventy-two of them with a single 1 and a single 2, and 84 of them with three 1's, all the rest in each case being a 0. It would seem best that each of these should be of equal probability, namely 1/165, in such a random generation.

  One way is to attack the problem with a brute force method such as the following:

 s = zeros(9,1);
 r = ceil(165*rand);
 if r <= 9 % Set a single element to 3
  s(ceil(9*rand)) = 3;
 else
  t = randperm(9,3);
  if r <= 9+72 % Set one element to 1 and another to 2
   s(t(1:2)) = [1;2];
  else % Set three elements to 1
   s(t) = 1;
  end
 end

The result will be in the sequence, s.

  Pablo, if you had in mind generalizing this problem, a more clever way of doing it than this would have to be devised. As you found, my own 'randfixedsum' routine would be of no use to you since it is designed for values within a real continuum. My intuition is that your problem in a general form would possess a similar complexity to 'randfixedsum' and therefore not easily forthcoming, though perhaps I am being too pessimistic.

Roger Stafford

Subject: Random Vectors with Fixed Sum / Naturals

From: Bruno Luong

Date: 12 Apr, 2012 06:17:20

Message: 5 of 9

"Roger Stafford" wrote in message <jm5mn3$g70$1@newscl01ah.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jm39qu$2mj$1@newscl01ah.mathworks.com>...
> > diff([0 sort(floor(4*rand(1,8))) 3])
> - - - - - - - - - -
> Using diff/sort in that manner does have a statistical shortcoming in my opinion, Bruno. One way to illustrate this is the following. The probabilities of each of the 3^8 = 6561 possible results of
>
> floor(4*rand(1,8))

Do you mean 4^8 = 65538 ?

>
> are equal. Out of these, just one of them is 3 3 3 3 3 3 3 3,

OK

> but seventy of them give four 3's and four 0's.

Do you mean the chance to get [3 3 3 3 0 0 0 0]? To me it equals to 1/4^8, just like [3 3 3 3 3 3 3 3] . I honestly can't see why the seventy comes from Roger. There is no permutation in my code.

Bruno

Subject: Random Vectors with Fixed Sum / Naturals

From: Bruno Luong

Date: 12 Apr, 2012 06:23:21

Message: 6 of 9

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jm5s1g$44p$1@newscl01ah.mathworks.com>...
>\ There is no permutation in my code.
>

Of course there is, it's SORT command.

I see the flaw now. Thanks

Bruno

Subject: Random Vectors with Fixed Sum / Naturals

From: Bruno Luong

Date: 12 Apr, 2012 06:53:18

Message: 7 of 9

Waht about this algo:

L1 = 3;
n = 9;

s = sort(randperm(n+L1-1,n-1));
v = diff([0 s n+L1]) - 1

Bruno

Subject: Random Vectors with Fixed Sum / Naturals

From: Roger Stafford

Date: 13 Apr, 2012 14:28:14

Message: 8 of 9

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jm5u4u$av2$1@newscl01ah.mathworks.com>...
> Waht about this algo:
>
> L1 = 3;
> n = 9;
>
> s = sort(randperm(n+L1-1,n-1));
> v = diff([0 s n+L1]) - 1
>
> Bruno
- - - - - - - - - -
  That's a great algorithm, Bruno! The "sort(randperm(p,q))" step has the interesting property of generating with equal probability all possible subsequences of 1:p possessing q elements. In the current problem with p = 11 and q = 8, there are 165 such subsequences. The step of "diff([0,s,p+1])" establishes a one-to-one relationship between these subsequences and the possible vectors of q+1 integers greater than or equal to 1 whose sum is p+1. By subtracting 1 we have q+1 whole numbers (>=0) whose sum is p-q, which gives the solution to the current problem.

  You caught me in an error, Bruno. It should indeed be 4^8 = 65536 and 4^9 = 262144. Fortunately for me, the 9+72+84 = 165 was still correct so the code I gave should be valid. However, yours is better, Bruno. It generalizes to any n non-negative integers whose sum is s.

  My use of the term "general form" referred to the case of n non-negative integers restricted to the range 0:m whose sum is s, which I believe is a much harder case that probably cannot be vectorized. Maybe you can prove me wrong on this, Bruno.

Roger Stafford

Subject: Random Vectors with Fixed Sum / Naturals

From: Bruno Luong

Date: 14 Apr, 2012 07:36:18

Message: 9 of 9

"Roger Stafford" wrote in message <jm9d5u$het$1@newscl01ah.mathworks.com>...

>
> My use of the term "general form" referred to the case of n non-negative integers restricted to the range 0:m whose sum is s, which I believe is a much harder case that probably cannot be vectorized.

I agree with you Roger. The upper bound introduced is a complication.

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