Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementation of a simple algorithm (to calculate probability)

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/

like image 528
James Avatar asked Dec 15 '09 19:12

James


People also ask

What is probability based algorithm?

In algorithmic information theory, algorithmic probability, also known as Solomonoff probability, is a mathematical method of assigning a prior probability to a given observation. It was invented by Ray Solomonoff in the 1960s. It is used in inductive inference theory and analyses of algorithms.


1 Answers

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.

like image 188
matt b Avatar answered Sep 25 '22 19:09

matt b