Random Number generation with Total sum.

3 ビュー (過去 30 日間)
Zahid Iqbal Rana
Zahid Iqbal Rana 2014 年 12 月 23 日
編集済み: John D'Errico 2014 年 12 月 28 日
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 件のコメント
Sean de Wolski
Sean de Wolski 2014 年 12 月 23 日
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 2014 年 12 月 28 日
編集済み: John D'Errico 2014 年 12 月 28 日
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.

サインインしてコメントする。

採用された回答

John D'Errico
John D'Errico 2014 年 12 月 23 日
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 件のコメント
Zahid Iqbal Rana
Zahid Iqbal Rana 2014 年 12 月 23 日
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 2014 年 12 月 23 日
編集済み: John D'Errico 2014 年 12 月 23 日
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.

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by