using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SharesAttack { class Program { static void Main(string[] args) { Console.Write("Enter the number of honest shares: "); var honest = int.Parse(Console.ReadLine()); Console.WriteLine(); Console.Write("Enter the number of shares you can buy: "); var moneyAllocation = int.Parse(Console.ReadLine()); Console.WriteLine(); const int Trials = 10000; var rand = new Random(); int unjustOwnedTBs = 0; for (int i = 0; i < Trials; ++i) { var moneyLeft = moneyAllocation; var totalNow = honest; while (moneyLeft > 0) { var wouldBeChosen = rand.Next(totalNow); while (wouldBeChosen < honest && moneyLeft > 0) { --moneyLeft; ++totalNow; wouldBeChosen = rand.Next(totalNow); } if (wouldBeChosen >= honest) ++unjustOwnedTBs; } } Console.WriteLine("Average unjust owned TB sequence until the fund is depleted: {0}", (double)unjustOwnedTBs / Trials); } } }