MATLAB: Monte Carlo Simulation – pair of dice roll

homeworkmonte carlo

Simulate rolling of a pair of dice over 10,000 trials and plot distribution of the # of rolls it takes to get "double 6" on a histogram graph.
I have attached the code I have written so far but need more help with defining parameters and plotting.

Best Answer

You are supposed to count the number of rolls it takes to get boxcars, not the number of boxcars you get in N rolls. So for each trial you are going to roll the dice as many times as it takes to get boxcars. Then you move on to the next trial. Your looping would look something like this:
for i=1:N
% code goes here to roll the dice over and over again until you get boxcars
% then record how many rolls it takes in an array.
end
So, instead of a single number count, make count an array with the definition
count(1) = the number of times it took only one roll to get boxcars
count(2) = the number of times it took two rolls to get boxcars
count(3) = the number of times it took three rolls to get boxcars
etc.
Suppose you use a variable called rolls inside your loop to keep track of the number of rolls it takes to get boxcars for that particular trial, then inside your loop you would at some point have this line when you finally get the boxcars:
count(rolls) = count(rolls) + 1;
Rolling the dice over and over again would probably be best with a while-loop. E.g.,
% initialize the rolls counter here
while( true )
% code to roll the dice one time here
% increment the rolls counter here
if( test to see if you got boxcars here )
% increment the appropriate counts element here
% exit the while loop
end
end
Does this outline make sense to you? Then after the for-loop you would just do this:
histogram(count);
Related Question