Using MATLAB Write a function diceRoll that takes in two numeric inputs. The fir
ID: 3595341 • Letter: U
Question
Using MATLAB
Write a function diceRoll that takes in two numeric inputs. The first number is how many dice you are rolling, and the second number is the size of the dice (4, 6, 8 and 10 sides, etc.). The function should return two parameters. The first returned parameter should be the total of all the rolls . The second returned parameter should be a vector of the value for each individual die rolled. Remember, rolling 5 6-sided dice is not the same as rolling one 6 sided die and multiplying by 5. The rand() function is useful.
Explanation / Answer
function [t,rolls] = diceRoll(n,sizen)
% initial total
t = 0;
% initial rolls
rolls = [];
% loop in range n
for i=1:n
% get a random integer between i, sizen
r = randi([1,sizen]);
% add random value to t
t = t+r;
% append random value to rolls
rolls = [r,rolls];
end
end
[a,b] = diceRoll(5,5)
% sample output
%a = 18
%b =
%
% 1 4 4 4 5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.