Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Birthday
- {
- class Program
- {
- static void Main(string[] args)
- {
- double numTries = 1E6;
- int maxPeople = 62;
- Random random = new Random();
- for (int numPeople = 2; numPeople <= maxPeople; numPeople++)
- {
- double matchingScenarios = 0;
- for (int numRun = 0; numRun < numTries; numRun++)
- {
- List<int> birthdays = new List<int>();
- for (int i = 0; i < numPeople; i++)
- {
- birthdays.Add(random.Next(365));
- }
- if (birthdays.GroupBy(b => b).Any(g => g.Count() > 1))
- {
- matchingScenarios++;
- }
- }
- Console.WriteLine("{0} people have a probability of {1:P}", numPeople, matchingScenarios / numTries);
- }
- Console.WriteLine("Press any key to exit.");
- Console.ReadKey();
- }
- }
- }
Add Comment
Please, Sign In to add comment