views:

456

answers:

6

EDIT: I've got it, thanks for all the help everyone! + Cleaned up post a little bit.

Also, this article was very helpful: http://www.codinghorror.com/blog/archives/001204.html?r=1183


Hi all,

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).

+7  A: 

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.

JSBangs
Oh right, so what I was basically doing was calculating the probability that whenever I draw a card, it is the black side facing up, which is 3/6.I've tried to implement it the way you described, but I get a probability of approx 0.75. I've probably done something wrong... Edited my initial post with new code.
James
You need to stop ignoring the red card and make the draw step more explicit in order to understand what's going on. @David Thornley and @matt b both have the right answer.The moral of the story is: don't skip steps in your algorithm just because you don't think they contribute to the result.
JSBangs
As to why you are getting 3/4 now - your code assumes that given a black-side up, the probablility of the other side being red or black is uniform - but it's not! That's exactly what you're trying to calculate! I would suggest taking a different approach completely, have your code simulate all possible events (not just the ones where the black side is up) and calculate the odds given those events.
matt b
Ok, thanks Matt I'll give that a go.
James
Thanks again for all your help JS Bangs, much appreciated :)
James
A: 

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.

pkaeding
+4  A: 

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.

David Thornley
+6  A: 

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.

matt b
Yes I've got it! Thank you very much Matt, I implemented your suggestion from scratch and I get the required probability of 2/3.Thanks again, I'm marking your answer as correct as it was the most helpful.
James
A: 

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.

VoiceOfUnreason
A: 

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.

Jerry Coffin