MATLAB: Finding probability distribution for the sum of 16 rolls- Efficient Method.

for loops

Hi,
I'm a beginner at matlab and a beginner in coding, so I was wondering if people could help me out with this problem that I'm experiencing. Basically I need to graph the probability distribution for sum of 16 rolls of a fair die, and so how I coded it was that I made 16 for-loops, however the code is taking too long to run and so I feel like I probably have to make my code more "efficient" except I'm not sure how I should go about doing this, like what function to use or how to code it.
I've attached my code for how I code the prob distr. for 8 rolls of a fair die. I basically used the same for and just added 8 more for loops, but the program is taking way to long to code, so I'm not sure how I can code it to make it run faster/actually run.
DValues = 1:6; %dice values
DTotal = 8:48;
Probsum = zeros(1, length(DTotal))
for x = DValues %Roll 1%
for y = DValues %Roll 2%
for z = DValues %Roll 3%
for w = DValues %Roll 4%
for a = DValues %Roll 5%
for b = DValues %Roll 6%
for c = DValues %Roll 7%
for d = DValues %Roll 8%
T = x + y + z+ w + a + b+ c + d;
for index = 1:length(DTotal)
if T==DTotal (index)
Probsum(index) = Probsum(index) + 1;
end
end
end
end
end
end
end
end
end
end
Probsum
plot(DTotal,Probsum/65536)
title('Probability Distribution for Sum of 8 Rolls of A Fair Die')
ylabel('Probability')
xlabel('Sum')

Best Answer

Do you really expect this to terminate in a (reasonably) finite amount of time?
6^16
ans =
2.8211e+12
So only about 3 trillion passes through those nested loops that you wanted. Instead, why not consider how you might interpret the following:
conv(ones(1,6)/6,ones(1,6)/6)
ans =
0.027778 0.055556 0.083333 0.11111 0.13889 0.16667 0.13889 0.11111 0.083333 0.055556 0.027778
Can a similar idea be used to find the distribution of the sum of 16 fair dice? (Yes, and it will take little time to run too.)
Ok, maybe the above was not sufficiently clear. So will this help?
format rat
conv(ones(1,6)/6,ones(1,6)/6)
ans =
1/36 1/18 1/12 1/9 5/36 1/6 5/36 1/9 1/12 1/18 1/36
Think about it. How would you extend that to work for 3 dice? 4 dice? 16 dice? Brute force is rarely a good idea for a homework assignment.