Monte Carlo Simulation- Simulating Sum of a DICE. Matlab CODE.

2.1k Views Asked by At

Hello everyone, I try to solve the following problem:

Use Monte Carlo simulation to approximate the sum of the 100 consecutive rolls of a fair die.

My work in math lab is:

sum=0;
roll= 100; 

for i = 1:roll    
    numbroll = ceil(6*rand);     
    sum=sum+numbroll;
end
sum

This code return the sum of 100 rolls. Back in the book the answer for this exerise said: enter image description here

Question: I don't know if I missunderstand the statement or not. What is the correct answer? and if someone know how to do the coding for a pair of dice I will be really apreciated.

Thanks for your help !

2

There are 2 best solutions below

2
On BEST ANSWER

Electro82 was definitely on the right track - just a couple of mods here and this works:

sum = 0;
roll = 100; 
distribution = zeros(1,12);

for i = 1:roll    
      dice_one = ceil(6 * rand);  
      dice_two = ceil(6 * rand);
      cursum = dice_one + dice_two;
      distribution(cursum) = distribution(cursum) + 1;
end

distribution/roll
0
On

As far I can do this code, I use Winther advice but need improve.Matlab said it is a problem with the **, part of the code. I hope it helps you a little bit.

sum=0;
roll= 100; 
distribution=zeros(12);

for i = 1:roll    
    dice_one = ceil(6*rand);  
    dice_two = ceil(6*rand);
    curmsum= dice_one+ dice_two
    **distribution(cursum)=distribution(cursum)+1**
end

hits(distribution/100)