{Red text entries in curly brackets are comments}

Procedure TForm1.SearchBtnClick(Sender: TObject) ;

 {We'll randomly select a million pancakes and keep 2 counts:
1: The number with a good side showing and
2: The number with good side showing that is also good on the hidden side
The ratio of these two numbers is the solution we're looking for}


Var
  i, TopSideGood, BackSideGood : integer;
{the variables}

begin

  TopSideGood:=0; {Initialize count of good sides seen}

  BackSideGood:=0; {Initialize count back sides that are also good}

  for i:=1 to 1000000 do  {for a million times do this loop}

  begin

{Randomly pick a pancake that is not showing burned side}

case random(3) of  {generate  and test a random number 0, 1, or 2 as a pancake type}

  {0 = Type 0 (burned, burned) - ignore it} 

  1: {Type 1: pancake - Good on one side}

     {50-50 chance that we see the good side, random(2) will return 0 or 1 equally}

     if random(2)=0 then inc(TopSideGood); {If random(2) = 0,  increment count of good sides selected}

  2: begin { Type2 pancake: Good on both sides}

        inc(TopSideGood); {We'll always see a good side for this type, count it}

        inc(BackSideGood); {Other side is always good also so count good back sides}

     end;

  end;

end;{end of trials loop}

with memo1.Lines do

begin  {Display results}

  Add('----------------------------------------------');

  Add(format('In %d trials, %d showed an OK side. ',[1000000, TopSideGood]));

  Add(format('Of these, %d (%.1f%%) were OK on the other side.',

             [BackSideGood, 100*BackSideGood/TopSideGood]));

  Add(#13+'While it is true that we will draw equal numbers of Types 1 and 2 pancakes, '

         +' half of the time we will be seeing the burned side of the Type 1''s and discard '

         +'without counting them thus counting twice as many Type 2''s as Type 1''s');

end;

  end;