Matlab 您需要滚动的次数期望是多少

Matlab 您需要滚动的次数期望是多少,matlab,Matlab,我有兴趣用Matlab写一些东西来模拟掷骰子的次数。(可能重新滚动只进行一次?) 我需要继续重新滚动模具,直到出现唯一的编号。 下面是我到目前为止的代码 感谢您的帮助 % N: the max number a roll of the fair die can take N = 6; % M: number of trials. Each trial is a sequence of rolls, and it % terminates once you see K N's in a

我有兴趣用Matlab写一些东西来模拟掷骰子的次数。(可能重新滚动只进行一次?)

我需要继续重新滚动模具,直到出现唯一的编号。 下面是我到目前为止的代码

感谢您的帮助

% N: the max number a roll of the fair die can take    
N = 6; 

% M: number of trials. Each trial is a sequence of rolls, and it
% terminates once you see K N's in a row.
M = 1; 
K = 1;

% initialize the trials vector. The m-th entry is going to store
% the number of rolls performed in the m-th trial.
trials = zeros(M,1);

% t0: record the start time of the simulation
t0 = clock();

% This for loop is to run the M trials.
for m = 1:M

    % collection: sequence of rolls. It's initialized with K
    % samples drawn from a uniformly distributed integer random
    % variable, because the minimal length of collection has to 
    % be K.

    % Here begins the loop to roll the die until all 6 numbers appear
    % If any number is repeated, re-roll until a unique number appears & move on to the next number
    collection = randi(N,K,1);  
    collection(:,2) = randi(N,K,1); 
    if collection(:,2)~=collection(:,1) 
        collection(:,3) = randi(N,K,1)  
    else collection(:,2) = randi(N,K,1)
    end

    if collection(:,3)~=(collection(:,1) && collection(:,2))
        collection(:,4) = randi(N,K,1)  
    else collection(:,3) = randi(N,K,1)
    end

    if collection(:,4)~=(collection(:,1)&& collection(:,2) && collection(:,3))
        collection(:,5) = randi(N,K,1)  
    else collection(:,4) = randi(N,K,1)
    end

    if collection(:,5)~=(collection(:,1)&& collection(:,2) && collection(:,3) && collection(:,4))
        collection(:,6) = randi(N,K,1)  
    else collection(:,5) = randi(N,K,1)
    end

    if collection(:,6)=(collection(:,1)&& collection(:,2) && collection(:,3) && collection(:,4) && collection(:,5))
        collection(:,6) = randi(N,K,1)  
    end

    % now that the last K rolls are all N's, we take note of the number 
    % of rolls performed in this trial
    trials(m) = length(collection);
end

% we measure how much time the simulation has spent by
% computing the time difference between now and t0
elapsed_time = etime(clock(), t0)

% the Monte Carlo estimate, which should be close to your analytical 
% solution.
mean(trials)

我会这样做的

function trials = diceExperiment( M )
trials = zeros(1,M);
for i = 1:M
    missingNum = true(1,6); % None of the six numbers are seen yet
    while (any(missingNum))
        currentNum = randi(6,1);
        trials(i) = trials(i)+1;
        missingNum(currentNum) = false;
    end
end
end % End of function

这个实验将运行M次,输出试验将告诉我每次需要多少骰子才能得到全部六个数字。你可以在这周围添加时钟来测量时间。

请解释代码应该做什么。你确定它必须是
集合(:,6)=(集合(:,1)…。
=
是一个赋值,即
集合(:,6)
将设置为右侧的值。比较将是
=
您确定它必须是
集合(:,3)~=(集合(:,1)&&collection(:,2))
?如果要检查第3列是否不同于第1列,也不同于第2列,则应将其写为
(集合(:,3)~=collection(:,1))&(集合(:,3)~=collection(:,2))
代码应模拟滚动模具,至少滚动一次,直到看到所有数字1-6为止。然后计算所取的卷数,按M重复此操作(试验次数)。最后,根据M次试验,取所需转鼓的平均数量。