实现一个简单的算法(计算概率)

发布于 2024-08-14 16:05:07 字数 571 浏览 2 评论 0原文

我被要求(作为作业的一部分)设计一个执行以下操作的 Java 程序:


基本上有 3 张卡片:

  • 两面都是黑色
  • 两面都是红色
  • 一侧是黑色,另一侧是红色

现在如果我随机取出一张卡片并将其放在桌子上。朝上的一面是黑色的。对方也是黑的概率是多少?

使用Java实现一个程序并尝试发现概率,该程序应该模拟纸牌戏法大量次,并应该输出纸牌的另一面是黑色的概率(它通过计算另一面有多少次来实现这一点)也是黑色的)。


然而我被告知我的代码是错误的(算法方面)......显然答案不应该是 0.50。我在尝试理解算法时犯了错误吗?

有人能指出我正确的方向吗? (我并不是要求您向我提供一个完整的工作实现,只是要求您提供算法应该如何工作)。


这篇文章非常有帮助:https://blog.codinghorror.com/finishing-the-game /

I've been asked (as part of homework) to design a Java program that does the following:


Basically there are 3 cards:

  • Black coloured on both sides
  • Red coloured on both sides
  • Black on one side, red on the other side

Now if I take a card randomly and place it on the table. The side facing up is black. What is the probability that the other side is also black?

Implement a program using Java and try to discover the probability, the program should simulate the card trick a large number of times and should output the probability that the other side of the card is black (it does this by counting how many times the other side also black).


However I've been told that my code is wrong (algorithm wise)... apparently the answer should not be 0.50. Have I made a mistake in trying to understand the algorithm?

Can anyone point me in the right direction please? (I'm not asking you to provide me with a fully working implementation, just on how the algorithm should work).


This article was very helpful: https://blog.codinghorror.com/finishing-the-game/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

骄傲 2024-08-21 16:05:07

这可能对算法没有帮助,但这就是我自己得出答案的方式:

当你随机抽一张牌并将其放在桌子上时,可能会发生六种同样可能的事情:

  1. 你选择 R/R 卡,然后将其红面朝上。
  2. 您选择 R/R 卡并将另一张红面朝上。
  3. 您选择 B/R 卡并将其黑面朝上放置。
  4. 您选择 B/R 卡并将其红面朝上放置。
  5. 您选择 B/B 卡并将其黑面朝上。
  6. 您选择 B/B 卡并将另一张黑色面朝上。

在这六个事件中,六分之三的结果是桌上有一张黑面朝上的牌。

在这 3 个事件中,正好有两个事件是黑牌的另一面。

因此,“对方也是黑的概率是多少?”这个问题的答案是这样的。是2/3

您的算法失败了,因为您仅将出现的 black_black 卡计算为单个事件,而实际上它是两个。

This might not help with the algorithm, but this is how I would derive the answer myself:

When you draw a random card and place it on the table, there are six equally probable things that could happen:

  1. You select the R/R card and place it red-side up.
  2. You select the R/R card and place the other red-side up.
  3. You select the B/R card and place it black-side up.
  4. You select the B/R card and place it red-side up.
  5. You select the B/B card and place it black-side up.
  6. You select the B/B card and place the other black-side up.

Of these six events, 3 out of 6 result in a black-side up card on the table.

Of these 3 events, in exactly two of them is the other side of the card black.

Therefore the answer to the question "What is the probability that the other side is also black?" is 2/3.

Your algorithm fails because you are only counting the black_black card coming up as a single event, when it is actually two.

岁月静好 2024-08-21 16:05:07

你的算法缺少一个关键步骤:把牌摆在桌面上。如果您抽出黑红卡,则不能保证当您放下它时黑色的一面会显示出来。添加一个额外的步骤来模拟随机选择每张卡片的一侧,然后确定有多少个案例显示黑色面孔,然后确定其中有多少个案例显示黑白卡片。

Your algorithm is missing a key step: putting the card on the table. If you draw the black-red card, there is no guarantee that the black side is showing when you put it down. Add an additional step to simulate selecting randomly one of the sides of each card, then determine how many cases show a black face, and then how many of those cases have the black-black card showing.

夏夜暖风 2024-08-21 16:05:07

牌有六个面,我们假设它们以相同的概率出现。三张脸都是黑色的,其中两张的另一面也是黑色的。我们丢弃所有红脸在最上面的情况,因此我们只关心三个等概率的黑脸。

因此,另一张脸是黑色的概率实际上是 2/3。

There are six sides to the cards, and we will assume them to come up with equal probability. There are three black faces, and two of them have black on the other side. We discard all cases in which a red face is uppermost, so we're only concerned with three black faces, of equal probability.

Therefore, the probability that the other face is black is in fact 2/3.

浅浅 2024-08-21 16:05:07

我认为你需要考虑红黑卡被抽出的两种可能性:红面朝上和黑面朝上。这些概率的总和就是红黑牌被抽到的概率。

I think you need to account for the two possibilities of the red-black card being drawn: red-side-up and black-side-up. The sum of these probabilities will be the probability that the red-black card is drawn at all.

烟花易冷人易散 2024-08-21 16:05:07

它可能有助于检查您已正确实现的问题:假设您随机选择了一张至少一侧为黑色的卡片,那么该卡片一侧为黑色而另一侧为红色的可能性有多大?

您没有正确实现的问题是:假设您正在查看一张这张牌的一面是黑色的,那么它在另一面是红色的几率有多大?

请注意,有两张黑牌,但有三张黑脸。

It may help to examine the problem that you have correctly implemented: given that you have randomly picked a card that is black on at least one side, what are the odds that the card is black on one side and red on the other?

The problem that you haven't implemented correctly is: given that you are looking at a card that is black on this face, what are the odds that it is red on the other face?

Note that there are two black cards, but three black faces.

海螺姑娘 2024-08-21 16:05:07

两面都是红色的牌基本上(请原谅这个双关语)是一条转移注意力的东西——无论怎样处理它,它都会变成红色,我们不需要再关心它了。

只剩下红/黑卡和黑/黑卡了。同样,如果红/黑是红面朝上,我们不需要进一步关注。其余的可能性是:

  1. 红/黑、黑上
  2. 黑/黑、第一面朝上
  3. 黑/黑、第二面朝上

Since two of those three have the other side black, the probability is two out of three.

The card with both sides red is basically (forgive the pun) a red herring -- any way it's dealt, it'll come up red and we don't need to care about it any more.

That leaves only the red/black and the black/black card. Again, we need pay no further attention if the red/black is dealt red side up. The remaining possibilities are:

  1. red/black, black up
  2. black/black, first side up
  3. black/black, second side up

Since two of those three have the other side black, the probability is two out of three.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文