Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.48 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Collections.Concurrent;
  7. using System.Threading;
  8.  
  9. namespace Lind.MontyHall
  10. {
  11.     class Program
  12.     {
  13.         protected static bool IsProcessing { get; set; }
  14.         protected static ConcurrentQueue<ContestResult> Results { get; set; }
  15.         protected static int Contests { get; set; }
  16.         protected static int Winners { get; set; }
  17.         protected static int Door1 { get; set; }
  18.         protected static int Door2 { get; set; }
  19.         protected static int Door3 { get; set; }
  20.         static void Main(string[] args)
  21.         {
  22.             Results = new ConcurrentQueue<ContestResult>();
  23.             IsProcessing = true;
  24.             Task t = Task.Factory.StartNew(() => ProcessQueue());
  25.            
  26.             Parallel.For(0, 100000, contestNumber =>
  27.             {
  28.                 Random random = new Random(contestNumber);
  29.                 int winningDoor;
  30.                 int[] doors = random.GetContest(out winningDoor);
  31.  
  32.                 int contestentChoice = random.Next(0, doors.Length);
  33.  
  34.                 int[] revealOptions = doors.Where(d => d != contestentChoice && d != winningDoor).ToArray();
  35.                 int revealDoor = random.GetRandomOrFirst(revealOptions);
  36.  
  37.                 int finalChoice = doors.Where(d => d != contestentChoice && d != revealDoor).First();
  38.                 Results.Enqueue(new ContestResult(finalChoice == winningDoor, winningDoor));
  39.             });
  40.             IsProcessing = false;
  41.             t.Wait();
  42.             Console.WriteLine("Door1: " + Door1 + " Door2: " + Door2 + " Door3: " + Door3);
  43.             Console.WriteLine("Switching door won " + Winners + "/" + Contests + " (" +
  44.                 ((double)Winners / Contests * 100) + "%)");
  45.         }
  46.  
  47.         protected static void ProcessQueue()
  48.         {
  49.             while (IsProcessing || !Results.IsEmpty)
  50.             {
  51.                 ContestResult result;
  52.                 if (!Results.IsEmpty && Results.TryDequeue(out result))
  53.                 {
  54.                     if (result.IsWinner)
  55.                         Winners++;
  56.                     switch (result.WinningDoor)
  57.                     {
  58.                         case 0: Door1++; break;
  59.                         case 1: Door2++; break;
  60.                         case 2: Door3++; break;
  61.                     }
  62.                     Contests++;
  63.                 }
  64.                 else if (Results.IsEmpty)
  65.                     Thread.Sleep(100);
  66.             }
  67.         }
  68.     }
  69.     public struct ContestResult
  70.     {
  71.         public ContestResult(bool isWinner, int door)
  72.         {
  73.             IsWinner = isWinner;
  74.             WinningDoor = door;
  75.         }
  76.         public bool IsWinner;
  77.         public int WinningDoor;
  78.     }
  79.     public static class Extensions
  80.     {
  81.         public static int GetRandomOrFirst(this Random random, int[] doors)
  82.         {
  83.             if (doors.Length == 1)
  84.                 return doors.First();
  85.  
  86.             return doors[random.Next(0, doors.Length)];
  87.         }
  88.         public static int[] GetContest(this Random random, out int winningDoor, int doorCount = 3)
  89.         {
  90.             List<int> doors = new List<int>();
  91.             int i = 0;
  92.             while (i < doorCount)
  93.             {
  94.                 doors.Add(i);
  95.                 i++;
  96.             }
  97.             winningDoor = random.Next(0, doorCount);
  98.             return doors.ToArray();
  99.         }
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement