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/
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:
- You select the R/R card and place it red-side up.
- You select the R/R card and place the other red-side up.
- You select the B/R card and place it black-side up.
- You select the B/R card and place it red-side up.
- You select the B/B card and place it black-side up.
- 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.
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.
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.
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.
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.
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:
- red/black, black up
- black/black, first side up
- black/black, second side up
精彩评论