C # verify the birthday paradox using a simulated program,

Source: Internet
Author: User

C # verify the birthday paradox using a simulated program,

The birthday paradox indicates that if there are 23 or more people in a room, the probability of two people having the same birthday is greater than 50%, which is about 50.7, this means that in a typical standard primary school class (30 persons), there is a higher possibility of two persons having the same birthday. For 60 or more people, this probability is greater than 99%. From the perspective of logical conflicts, the birthday paradox is not a paradox. in the sense that this mathematical fact conflicts with General intuition, it can be called a paradox. Most people think that the probability of two people with the same birthday in 23 should be much less than 50%.

This result can be calculated using mathematical formulas. Instead of using the formula, we can directly use a program to simulate the generation of a room, which contains 23 persons and check whether there are birthday duplicates. Repeat multiple times to accumulate the results. The following is the source code (console program)

Using System; using System. threading; namespace ConsoleApplication1 {class Program {// a date, a random device, used to generate a random birthday private static DateTime dt = Convert. toDateTime ("12-31"); private static Random ran = new Random (); /// <summary> /// number of rooms /// </summary> private static int roomNumber = 0; /// <summary> /// number of qualified rooms /// </summary> private static int roomRight = 0; /// <summary> /// number of attempts /// </summary> private Static int test = 0; static void Main (string [] args) {Console. writeLine ("Enter the number of executions, which can only be an integer"); try {test = Convert. toInt32 (Console. readLine (); Thread t1 = new Thread (new ThreadStart (start); t1.Start ();} catch {Console. writeLine ("incorrect input. Please run it again. ");} Console. readLine () ;}//< summary> /// start the simulated test /// </summary> private static void start () {for (int I = 0; I <test; I ++) {DateTime [] dtRoom = creatRoom (); if (checkBirsday (dtRoom) = true) roomRight ++; Console. writeLine ("remaining:" + (test-I ). toString ();} float probability = Convert. toSingle (roomRight)/Convert. toSingle (roomNumber); Console. writeLine (String. format ("A total of {0} rooms, matching {1} rooms, probability {2} ", roomNumber, roomRight, probability) ;}/// <summary> // generate a room, contains 23 random birthdays /// </summary> /// <returns> </returns> private static DateTime [] creatRoom () {DateTime [] people = new DateTime [23]; for (int I = 0; I <people. length; I ++) {people [I] = creatBirsday ();} roomNumber ++; return people ;} /// <summary> /// randomly generate a date /// </summary> /// <returns> </returns> private static DateTime creatB Irsday () {int RandKey = ran. next (0,365); DateTime dtBirsday = dt. addDays (RandKey * (-1); return dtBirsday ;} /// <summary> /// check whether there are duplicate birthdate items in the room /// </summary> /// <param name = "dtBrisday"> </param> // /<returns> </returns> private static bool checkBirsday (DateTime [] dtBrisday) {for (int I = 0; I <dtBrisday. length; I ++) {int id = Array. indexOf (dtBrisday, dtBrisday [I]); // here 1 is the value you want to find if (id! =-1 & id! = I) return true;} return false ;}}}View Code

Running result

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.