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:
Simulation of 5 Random Variables with Sum Constraint

Subject: Simulation of 5 Random Variables with Sum Constraint

From: Siddhartha

Date: 27 Mar, 2013 16:45:05

Message: 1 of 1

Let's say I have 5 random variables that are discrete. Each one of them can possess a low, base and high value somewhere between 15 and 35. I know their sum can never exceed 98. What's the best way to simulate this? Is the rejection method good? The sudden drop in the pdf of the sum bothers me, not sure if that's right, but it maybe.


n = 5;
a(n,3) = 0;
for i = 1:n
    a(i,1:3) = [100/(1.5*n)+(-1)^fix(rand)*rand*(100/(2*n)) 100/(1.1*n)+(-1)^fix(rand)*rand*(100/(2*n)) 100/(0.8*n)+(-1)^fix(rand)*rand*(100/(2*n))];
end


d = lhsdesign(n,10000);

e = 0;
b(1,10000)=0;
for i = 1:n
    b(d(i,:)<0.3) = a(i,1);
    b(d(i,:)>0.7) = a(i,3);
    b(b==0) = a(i,2);
    e = e + b;
end

f = e(e<98);
[~,cole] = size(unique(e));
[~,colf] = size(unique(f));

subplot(2,2,1), hist(e,cole);
subplot(2,2,2), hist(e(e<98),colf);
subplot(2,2,3), ecdf(e);
subplot(2,2,4), ecdf(e(e<98));

Tags for 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