Skip to main content

Implementation of a simple algorithm (to calculate probability)


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


Source: Tips4all

Comments

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

    ReplyDelete
  2. 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.

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

    ReplyDelete
  4. 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.

    ReplyDelete
  5. 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.

    ReplyDelete
  6. 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 upblack/black, first side upblack/black, second side up
    Since two of those three have the other side black, the probability is two out of three.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex