MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
Asked by Ivan on 25 May 2013

I have trouble with me homework.

so the problem is this: i have n fair dices with 6 sides that roll all together and i need to calculate the probability of sum of dices side.

i need to write script when:

function prob = sumDicePDF(n,k) input: 1. number of dices(n >= 1) 2. k - integer number from n to 6n output: probability

example: sumDicePDF(3,4) ans = 0.0139 sumDicePDF(8,20) ans = 0.0218

i need to use equalition: probability = sigma[1:6](P(sum(n-1) = k - i) * 1/6

and i forbiden to use recursion so my script run time must be near n^2

i understood the problem but i cant find a way to biuld the loop for counting probability so my answer be prob=(1/6)^n*sumOfCounting;

so my quastion is how to make loop to find sumOfCounting.

i wrote:

vec = ones(1,n - 1); sumOfCounting = 0; for i = 1:6 for j = 1:length(vec) vec(j) = i; sumOfvalue = sum(vec); if k - sumOfvalue < 7 && k - sumOfvalue > 0 for m = 1:length(vec) sumOfCounting = sumOfCounting + length(vec) + 1 - m; end end end end

but it not counting all options so where is the problem?

thank and best wishes.

*No products are associated with this question.*

Answer by Image Analyst on 25 May 2013

Accepted answer

Nice try, but why not just use the sum() function - that's what it's there for:

% Roll the dice "numberOfRolls" times numberOfRolls = 200; % Number of times you roll all 6 dice. n = 10; % Number of dice. maxFaceValue = 6; rolls = randi(maxFaceValue, n, numberOfRolls)

% Sum up dice values for each roll. columnSums = sum(rolls, 1) % Find out how many times each sum occurred. edges = min(columnSums):max(columnSums) counts = histc(columnSums, edges) % Normalize grandTotalSum = sum(counts(:)) normalizedCountSums = counts / grandTotalSum bar(edges, normalizedCountSums, 'BarWidth', 1); grid on; % Enlarge figure to full screen. set(gcf, 'units','normalized','outerposition',[0 0 1 1]); % Give a name to the title bar. set(gcf,'name','Demo by ImageAnalyst','numbertitle','off') title('Frequency of Roll Sums', 'FontSize', 40);

Show 2 older comments

Ivan on 30 May 2013

Thank i found the way to answer it. if its interesting the function i was trying to write is:

% function: sumDicePDF % purpose: Calculate the probability function of the sum of dice % Given n fair dice. Let define X(i) the value % out in the cube (i). % Define Y(n) = sum(X(i)). % Calculate the probability function of Y(n). % Means the P(Y(n) = k) % for given k. % input: n - number of dices. n >= 1 (integer) % k - integer betwen n and 6n % output: probability - P(Y(n) = k) % example: sumDicePDF(3,4) ans = 0.0139 % sumDicePDF(8,20) ans = 0.0218

function prob=sumDicePDF(n,k) %Check the input if n < 1 || k < n || k > 6*n display('Bad Input! Try Again.'); n = input('n = '); k = input('k = '); prob=sumDicePDF(n,k); return; end %If there is only one dice return probability 1/6 if n == 1 prob = 1/6; return; end

%We will use equalition: f(n,k)=sigma(i = 1:6)[f(n-1,k-i)*1/6]. The %idea is to create matrix (f(1,1) f(1,2) ... f(1,6n)) % (...... ...... ... .......) % (f(n,1) f(n,2) ... f(n,6n)) %And use it to find out f(n,k) that we need with equalition

valuesMatix = zeros(n - 1,6*n); %Define new matrix size n-1,n*6 %with zeros %Fill the first row for 1<x<6 with 1/6 and the rest leave with zeros for i = 1:6 valuesMatix(1,i) = 1/6; end %Fill the rest of matrix with relivant values for i = 2:n - 1 for j = i:6*i probForSpecPlace = 0; for l = 1:6 if j - l > 0 %Check if value out of range becouse %(k - i) must be positive %Sum the Probabilites for specific f(n,k) probForSpecPlace = probForSpecPlace + valuesMatix(i - 1,j - l)*1/6; end end %Put that value in its place in matrix for futher use valuesMatix(i,j) = probForSpecPlace; end end prob = 0; %Define the probability to 0 %The last module: find the exact value for wanted probability using %the equalition %by using the created matrix of values for i = 1:6 if k - i > 0 prob = prob + valuesMatix(n - 1,k - i)*1/6; end end end

so it work like i wanted and for prove:

feature accel off; tic; sumDicePDF(8,20) , toc; feature accel on; ans = 0.0218 Elapsed time is 0.004089 seconds.

Thanks a lot for helping.

Image Analyst on 30 May 2013

With lots of nested for loops, it doesn't look as efficient as mine, which I timed at 0.001 seconds, but whatever ... it's only 3 milliseconds (not noticeable at all) and it's your homework and if you understand yours better that's fine. At least I introduced you to a vectorized, fast way of doing it.

Ivan on 30 May 2013

You write but, your code make use of randomize, and its note needed. Becouse player already rolled his dices and get the sum, and he asked what was the probability that would get it for example:

he rolled 5 dices and get 20, so he asked me what was probability to get 20 by rolling 5 dices, answer is: 0.0837.

List of probability's for 1 to 25 dices can be fount here: http://wizardofodds.com/gambling/dice/2/

Still worked on your code: (Made i little change):

function normalizedCountSums=testMine(n,k) % Roll the dice "numberOfRolls" times numberOfRolls = 200; % Number of times you roll all 6 dice. %n = 10; % Number of dice. maxFaceValue = 6; rolls = randi(maxFaceValue, n, numberOfRolls); % Sum up dice values for each roll. columnSums = sum(rolls, 1); % Find out how many times each sum occurred. edges = min(columnSums):max(columnSums); counts = histc(columnSums, edges); % Normalize grandTotalSum = sum(counts(:)); normalizedCountSums = counts(k) / grandTotalSum; % bar(edges, normalizedCountSums, 'BarWidth', 1); % grid on; % % Enlarge figure to full screen. % set(gcf, 'units','normalized','outerposition',[0 0 1 1]); % % Give a name to the title bar. % set(gcf,'name','Demo by ImageAnalyst','numbertitle','off') % title('Frequency of Roll Sums', 'FontSize', 40); end

the output was:

>> feature accel off; tic; testMine(8,20) , toc; feature accel on;

ans =

0.0250

Elapsed time is 0.003182 seconds. >> feature accel off; tic; testMine(8,20) , toc; feature accel on;

ans =

0.0300

Elapsed time is 0.001315 seconds.

Its Fast but the output is little wrong, it must be the same all the time = 0.0218

Maybe i wrote the problem not so good. but still thanks used it for other little program. :)

## 0 Comments