【发布时间】:2018-12-25 03:55:19
【问题描述】:
我必须创建应该显示获得黑桃 A 和任何 2 的概率的代码。主要问题是我可以编写显示获得黑桃 A 和黑桃 2 概率的代码,即 1/52 * 1/51。我不能得到 1/52 和 4/51,我如何得到这些概率?
这是我到目前为止的代码
M = 100000; %number of MC experiments to run
N = 0; %number of successful MC experiments
P = 0; %probability
figure(1); %create a new figure window
hold on; %hold all plots
%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
while pos2 == pos1
pos2 = randi(52);
endwhile
if (deck(pos1) == 1 && deck(pos2) == 2)
N +=1; %increment number of successful experiments
endif
plot(i,N/M,'r*') %plot probability of successful experiments thus far
endfor
hold off; %release all plots
P = N/M; %calculate probability
format long %prefer long format
disp('Probability of drawing Ace of Spades and a 2 is:'), disp(P)
【问题讨论】:
-
1) Matlab ~= Octave 2) “只有一次我将不得不使用 octave...”,这很大胆,你不认为你的教授有选择 Octave 的理由吗? 3)为什么你们都随机化一副牌并从这副牌中随机抽牌? 4) 将卡片标记为 1 = AH, 2 = 2H, ..., 13 = KH, 14 = AC, 15 = 2C,... H 是红心,C 是梅花等,是不是更有意义。这也应该使您的检查更容易。
-
问题已解决。谢谢
标签: math octave simulation probability montecarlo