【问题标题】:Monte Carlo simulations involving mutiple probability涉及多个概率的蒙特卡罗模拟
【发布时间】: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


【解决方案1】:

最初是一条评论,但超过了字数。更多的是提示而不是答案:

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

一旦你明白了这一点,只需洗牌并查看最上面的两张牌(没有理由进一步随机选择牌,pos1pos2 毫无意义:只需使用 1,2) .在您的编码下,第一名是黑桃王牌吗?下一个是你编码下的2吗?

【讨论】:

  • 感谢您的建议。我会试试的
  • 我已经解决了这个问题。你的建议很有帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-01
  • 1970-01-01
  • 2012-04-26
  • 2015-02-10
相关资源
最近更新 更多