这是我们用来确定抽出两张理想卡的可能性的代码: %从52张牌中模拟一系列抽奖 %使用蒙特卡罗方法
M = 1000; %number of MC experiments to run
N = 0; %number of successful MC experiments
P = 0; %probability
%start experiment loop
for i=1:M
deck = randperm(52)'; %generate deck of cards, 1x52 vector
pos1 = randi(52); %select position to draw from randomly
pos2 = randi(52); %select position to draw from randomly
%check if pos1==pos2; if so, select another pos2
while pos2 == pos1
pos2 = randi(52);
endwhile
%check if card is desired card; in this case AS = 1; checks at random position in deck
%2: now checking for Pr[AS and 2S]
if (deck(pos1) == 1 && deck(pos2) == 2)
N +=1; %increment number of successful experiments
endif
%deck=[]; %reset deck
endfor
P = N/M; %calculate probability
format long %prefer long format
disp('Probability of drawing Ace of Spades and 2 of Spadess is:'), disp(P)
但是,我想要的只是所需结果的数量,即绘制2张黑桃的次数。