Guest User

Untitled

a guest
Jan 16th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.14 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Birthday
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             double numTries = 1E6;
  12.             int maxPeople = 62;
  13.  
  14.             Random random = new Random();
  15.  
  16.             for (int numPeople = 2; numPeople <= maxPeople; numPeople++)
  17.             {
  18.                 double matchingScenarios = 0;
  19.                 for (int numRun = 0; numRun < numTries; numRun++)
  20.                 {
  21.                     List<int> birthdays = new List<int>();
  22.                     for (int i = 0; i < numPeople; i++)
  23.                     {
  24.                         birthdays.Add(random.Next(365));
  25.                     }
  26.                     if (birthdays.GroupBy(b => b).Any(g => g.Count() > 1))
  27.                     {
  28.                         matchingScenarios++;
  29.                     }
  30.                 }
  31.                 Console.WriteLine("{0} people have a probability of {1:P}", numPeople, matchingScenarios / numTries);
  32.             }
  33.             Console.WriteLine("Press any key to exit.");
  34.             Console.ReadKey();
  35.         }
  36.     }
  37.  
  38. }
Add Comment
Please, Sign In to add comment