Monte Carlo Simulation - pair of dice roll

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.

1 Commento

Sorry, unsure why the picture is backwards. This is what I wrote:
N = 10,000; %number of trials
count = 0;
for i=1:N
throws1 = randi(6,1)
throws 2 = randi(6,1);
if throws1 & throws 2 == 6
fprintf('double 6')
count = count + 1;
end
end

Accedi per commentare.

 Risposta accettata

James Tursa
James Tursa il 26 Apr 2020
Modificato: James Tursa il 26 Apr 2020
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);

2 Commenti

yes the outline makes sense I came up with this. I was wondering how to initialize the counter_vector to record the number of roll necessary to produce the desired outcome
Always post code as text that we can copy and test. We can't copy and test a picture.
For initilizing counter_vector, since you don't know ahead of time how big it needs to be, for this problem I would just let it dynamically size itself during the for-loop. So initialize it like this:
counter_vector = [];
And this line
while success_flag = 0 % single = is assignment, wrong here
should be this
while success_flag == 0 % double == is equality test
Inside the for-loop and immediately before the while-loop starts you need to re-initialize the roll counter to 0 because you are starting a new trial:
roll_counter = 0;
This line
if roll1 && roll2 == 6
needs to be individual tests
if roll1 == 6 && roll2 == 6
And inside this if-test is where you would increment the appropriate element of counter_vector.

Accedi per commentare.

Più risposte (1)

David Hill
David Hill il 26 Apr 2020
Modificato: David Hill il 26 Apr 2020
x=sum(randi(6,10000,2),2);
y=find(x==12);
y=diff([0,y]);
histogram(y,'BinMethod','Integers');

Categorie

Scopri di più su Mathematics in Centro assistenza e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by