我需要写一个功能来滚动一个4面模具N次并返回4次滚动的数量。我想在我的Matlab函数中使用randperm和for lop。到目前为止我有:
function [value2] = rollDie( N )
%This function inputs the number of times a 4-sided die is rolled and
%returns the number of times a 2 was rolled.
% N = the number of times the die was rolled
x = ranperm(4);
v = x(1);
count = 0;
for v = 2
count = count + 1;
end
我不确定这开头是否正确,但我也无法弄清楚如何根据滚动次数的输入使该函数运行N次。
答案 0 :(得分:2)
您可以使用randi
或随机整数生成器创建1到6之间的随机向量。您的代码看起来像
function count = rollDire(N) % Produce N random rolls of a 6-sided die rolls = randi(6,1,N); % Count number of times we encounter the number 4 count = sum(rolls == 4);