涉及多重概率的蒙特卡洛模拟

时间:2018-07-17 12:32:51

标签: math octave simulation probability montecarlo

我必须创建代码,该代码应该显示获得黑桃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 个答案:

答案 0 :(得分:2)

最初为注释,但超出了字数。更多提示而不是答案:

首先,您应该清楚自己的编码。黑桃王牌对应什么?什么对应于2? 1:52到底如何映射到甲板上?我想不出自然的编码方式,其中数字1对应于黑桃A,数字2对应于作为2的卡。原则上的解决方案是使用商和除以4的余数来确定秩和。分别适合。便宜但可使用的解决方案(即使不是很自然)是让1对应于黑桃a,数字2,3,4,5对应于4的二(不指定其余的编码)。

一旦您想到了这一点,只需洗牌并看一下前两张牌(没有理由进一步随机选择牌,pos1pos2就毫无意义:只需使用1,2)。在您的编码下,最顶尖的是黑桃王牌吗?下一个是您编码的2吗?

相关问题