MATLAB: Monte carlo simulation in matlab two dices roll

homeworkmonte carlo simulation in matlab two dices roll

if 2 dices were thrown & there top value were added ,what is the probability of getting a sum of 7 ?

Best Answer

n = 10; % Number or trials
throw = randi(6, n, 2); % Value of throws, 2 dice
SumThrow = sum(throw, 2); % Sum of both dice
Match = (SumThrow == 7); % Logical array: 1 if sum of values is 7
count = sum(Match); % Number of matching throws
Or with modifications of your code:
count = 0;
for i=1:10
throws1 = randi(6, 1);
throws2 = randi(6, 1);
sumThrow = throws1 + throws2; % Do not use "sum" as name of a variable
if sumThrow == 7
fprintf('sum is 7\n')
count = count + 1;
else
fprintf('false\n')
end
end
fprintf('Number of throws with sum=7: %d\n', count);