Write a script using matlab that rolls a pair of weighted dice twenty times and
ID: 3168134 • Letter: W
Question
Write a script using matlab that rolls a pair of weighted dice twenty times and prints the sum each time. The 20 numbers should be the only output (so make sure to use semicolons ; as needed). Also make sure your code is commented (making sure to indicate what each variable is used for, and what happens in each major are of the code).
For one dice roll, the following should be done:
roll die 1, which has a 15% chance of rolling either a 1, 2, 3, 4 or 5 and a 25% chance of rolling a 6
roll die 2, which has a 15% chance of rolling either a 1, 2, 3, 5 or 6 and a 25% chance of rolling a 4
This will result in two numbers (one from each die). Add these numbers up, and this is the roll of the dice total. This should be done twenty times.
Hints: use the rand function to generate a random number between 0 and 1, then use this to determine the roll of each die using a conditional statement. Use some kind of loop to do this 20 times, and then make sure your script executes with a clean workspace and only outputs the 20 rolls.
Explanation / Answer
%%%% Matlab code %%%%%
clc;
clear all;
close all;
format short
%%% restoredefaultpath %%%
for k=1:20
%%%% getting number on first dice
n=rand(1,1);
if (n<=0.15)
r1=1;
else if (0.15 < n && n <=0.15)
r1=2;
else if (0.3 < n && n <=0.45)
r1=3;
else if (0.45 < n && n <=0.6)
r1=4;
else if (0.6 < n && n <=0.75)
r1=5;
else if (n > 0.75 )
r1=6;
end
end
end
end
end
end
%%%% getting number on second dice
n=rand(1,1);
if (n<=0.15)
r2=1;
else if (0.15 < n && n <=0.15)
r2=2;
else if (0.3 < n && n <=0.45)
r2=3;
else if (0.45 < n && n <=0.70)
r2=4;
else if (0.70 < n && n <=0.85)
r2=5;
else if (n > 0.85 )
r2=6;
end
end
end
end
end
end
s(k)=r1+r2;
end
disp('output of 20 rools is :' );
s'
OUTPUT:
output of 20 rools is :
ans =
10
2
6
6
7
9
7
7
10
10
9
9
5
5
12
5
5
4
9
7
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.