How can I edit my code to produce a function that allows the user to choose the number of 6-sided die they want to roll, and have an optional argument to add them all together? The result should be an array of "rolled" numbers, and the added value.

9 visualizaciones (últimos 30 días)
function Dice = rollDice(rolls,add)
time = clock();
seed = floor(time(end) * 1000);
rng(seed);
number = floor(rand * 6) + 1;
count = 0;
if isempty(rollDice)
rolls = 2;
end
if rolls == 0
msg = 'You didn''t roll any die';
warning(msg);
end
Dice = zeros(rolls);
for n = 1:a
count = count + 1;
Dice(n) = number;
end
if nargin(rollDice) < 2
add = 0;
else
add = sum(Dice);
end
disp(count);
disp(number);
  1 comentario
Caglar
Caglar el 13 de Nov. de 2018
function [Dice,Sum_of_rolls] = rollDice(rolls,add)
time = clock();
seed = floor(time(end) * 1000);
rng(seed);
for d=rolls:-1:1 %reverse loop to use memory better
Dice(d)=floor(rand * 6) + 1; %You need to call rand for each roll to get different values.
end
if exist('add','var') %I think checking for the variable instead of nargin is cleaner. You can also add '&& add==1'.
Sum_of_rolls=sum(Dice);
else
Sum_of_rolls=0;
end
end

Iniciar sesión para comentar.

Respuestas (1)

Steven Lord
Steven Lord el 13 de Nov. de 2018
I have several comments on your code.
function Dice = rollDice(rolls,add)
time = clock();
seed = floor(time(end) * 1000);
rng(seed);
Rather than trying to generate the seed on your own, I would just call rng with the 'shuffle' input.
I would also consider having rollDice accept a third optional input argument. If that input is present, I'd use that in the rng call instead of calling rng('shuffle'). This will allow you to reproduce a set of dice rolls if necessary.
number = floor(rand * 6) + 1;
I recommend using randi instead.
count = 0;
if isempty(rollDice)
There's no variable named rollDice in your function, so this will error. From context I think you're using this to detect if the user called this function with 0 input arguments and rolling a pair of dice in that scenario. If so, I'd use nargin as you did below when you detected whether or not the user had specified the add input argument.
rolls = 2;
end
if rolls == 0
msg = 'You didn''t roll any die';
warning(msg);
end
Dice = zeros(rolls);
for n = 1:a
count = count + 1;
Dice(n) = number;
This is the Dilbert random number generator or the xkcd random number generator. This does not call rand or randi each time you refer to number; it instead assigns that same value you generated up above into the element of Dice.
You can also roll all the dice at once. See the "Matrix of Random Numbers" example on the documentation page for the rand function. The "Random Integers" example on that page will also be of interest to you.
end
if nargin(rollDice) < 2
add = 0;
else
add = sum(Dice);
end
disp(count);
disp(number);
You passed add into the function as an input, but you don't return it from the function as an output. So that variable gets thrown away when your function returns. See this documentation page for an explanation of how to define your function so it returns multiple output arguments.
From a style perspective, I'd also use a different variable name for the input argument that controls "should I return the sum?" and the output argument that contains the sum.
  3 comentarios
Steven Lord
Steven Lord el 13 de Nov. de 2018
Your function is defined to accept at most one input argument, so if nargin > 1 can never be satisfied.
You could use if nargout > 1 to check the number of outputs, or you could compute the sum without checking whether or not the user asked for the second output. If you compute add but the user doesn't call the function with 2 outputs the value assigned to add will be thrown away when the function exits.
If the computation of the add variable was expensive (in terms of memory and/or time) avoiding computing it if it was not asked for could be a good idea, but you'd have to "roll" a lot of dice for that one sum call to be the bottleneck in your code.

Iniciar sesión para comentar.

Categorías

Más información sobre Programming en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by