Random Number generation with Total sum.

2 views (last 30 days)
clear all ; clc Demand=300; N=10; D=3;
a(1:N,1)=100; b(1:N,1)=600; %bounds on variable 1
a(1:N,2)=100; b(1:N,2)=400; %bounds on variable 2
a(1:N,3)=50; b(1:N,3)=200; %bounds on variable 3
x=a+(b-a).*rand(N,D);
I want to generate random numbers whose sum is equal to Demand=300 and their bounds are specified. When I generate by this code their sum is not equal to Demand, Please tell me what to do ?
Note-> I have studied randfixedsum function in Matlab help but it doesn't help me.
  2 Comments
Sean de Wolski
Sean de Wolski on 23 Dec 2014
Please don't start new questions for the same thing as an existing one.
Instead, answer my question that I posed to you:
WHY DOESN'T RANDFIXEDSUM HELP YOU?
John D'Errico
John D'Errico on 28 Dec 2014
Edited: John D'Errico on 28 Dec 2014
Sean - randfixedsum assumes the points lie in a hyper-cube, not a hyper-rectangle. The question here has lower and upper limits that differ for each variable. The randfixedsum code assumes scalar a and b.

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 23 Dec 2014
Hacking randfixedsum to allow a different set of bounds is probably more difficult than you want to do.
I would also point out that your bounds on the variables for this sum are way too large. For example, if the total sum is 300, AND the lower bounds are respectively [100 100 50], then variable 1 can never be larger than 150, since variables 2 and 3 must be at least 100 and 50 respectively.
Likewise, variable 2 also has an upper limit of 150, and variable 3 may never exceed 100, by the same logic.
Irrespective of those minor issues, the set of points you wish to generate fall inside a triangle in the R^3 (x,y,z) domain. I've plotted that triangle here.
It has corner vertices of:
A = [150 100 50];
B = [100 150 50];
C = [100 100 100];
All that you need to do is to generate a set of random points that lie in that triangle, and are uniformly distributed. (Note that had your equality constraint been a bit higher, then the locus of points might have fallen in a more complex region in the (x,y,z) space. Or if the dimension of the space is higher than 3, it also gets more complex to solve. But your specific problem is quite easy to solve, even trivial.)
N = 10;
t = rand(N,1);
s = sqrt(rand(N,1));
xyz = bsxfun(@times,s,t*A + (1-t)*B) + (1-s)*C;
Sadly, I am confident that your real problem is more complex, and you have just given this as an example. If that is true, then you should have said so of course, but nobody ever does.
  2 Comments
Zahid Iqbal Rana
Zahid Iqbal Rana on 23 Dec 2014
ok I got your answer and thank you so much for this. But if Demand = 850 than how to solve?
John D'Errico
John D'Errico on 23 Dec 2014
Edited: John D'Errico on 23 Dec 2014
Then the points lie in a pentagonal planar subspace (polygonal with 5 sides), which in turn can be broken into 3 triangles. Sample from the 3 triangles with probability that is proportional to their areas.
Essentially, you can think of the sum constraint as a plane, and the bound constrained set as a box in 3-d. The intersection of a plane and a box in 3 dimensions can be anything from a simple triangle up to a six sided polygon, depending on how the plane slices through that box.

Sign in to comment.

More Answers (0)

Categories

Find more on Computational Geometry in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!