fork(1) download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace SharesAttack
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. Console.Write("Enter the number of honest shares: ");
  13. var honest = int.Parse(Console.ReadLine());
  14. Console.WriteLine();
  15. Console.Write("Enter the number of shares you can buy: ");
  16. var moneyAllocation = int.Parse(Console.ReadLine());
  17. Console.WriteLine();
  18. const int Trials = 10000;
  19. var rand = new Random();
  20. int unjustOwnedTBs = 0;
  21.  
  22. for (int i = 0; i < Trials; ++i)
  23. {
  24. var moneyLeft = moneyAllocation;
  25. var totalNow = honest;
  26.  
  27. while (moneyLeft > 0)
  28. {
  29. var wouldBeChosen = rand.Next(totalNow);
  30. while (wouldBeChosen < honest && moneyLeft > 0)
  31. {
  32. --moneyLeft;
  33. ++totalNow;
  34. wouldBeChosen = rand.Next(totalNow);
  35. }
  36.  
  37. if (wouldBeChosen >= honest)
  38. ++unjustOwnedTBs;
  39. }
  40. }
  41.  
  42. Console.WriteLine("Average unjust owned TB sequence until the fund is depleted: {0}", (double)unjustOwnedTBs / Trials);
  43. }
  44. }
  45. }
  46.  
Success #stdin #stdout 0.61s 33912KB
stdin
1000
1000
stdout
Enter the number of honest shares: 
Enter the number of shares you can buy: 
Average unjust owned TB sequence until the fund is depleted: 499.8315