Author Topic: Same Birthday Within A Group  (Read 3633 times)

0 Members and 1 Guest are viewing this topic.

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
Same Birthday Within A Group
« on: April 28, 2020, 06:01:26 am »
The likelihood of two people within a group in a room having the same birthday is higher than you first might think.  For example, in a group of just 23 people there is about a 50–50 chance that two of them will have the same birthday.

This apparently high likelihood has to do with all the possible combinations, rather than 1/365 times 23.  The mathematicians amongst us will understand this properly and will be aghast at my being unable to explain it clearly.  I'm only an engineer and can't be expected to understand this stuff!

So, I thought that I'd test this out, which is easily done in code.  Generate an array of "people" each with a random birthday (the code even takes into account February 29th) and then test all pairs for the same birthday.  Mathematicians have derived the probability from theory, and the program plots the results against this theory.

You will not be surprised that the output is very close to theory - isn't mathematics wonderful?  You will see the program results plotted along the same curve as the theory (you have to look closely) with some vertical error derived from sampling errors.

You will need the image file.

Code: QB64: [Select]
  1. 'Who has the same birthday?
  2. 'Image from edscave.com
  3.  
  4. CONST False = 0, True = NOT False, Qty% = 3000
  5.  
  6. DIM Days%(1460)
  7.  
  8. 'Populate an array with equal probability of 1 to 365, with 366 having a quarter of this probability
  9. 'This accounts for February 29th
  10. FOR N% = 0 TO 3
  11.     FOR M% = 0 TO 364
  12.         Days%(N% * 365 + M%) = M% + 1
  13.     NEXT M%
  14. NEXT N%
  15. Days%(1460) = 366
  16.  
  17. SCREEN _NEWIMAGE(600, 400, 32)
  18. COLOR _RGB32(255, 100, 0), _RGB32(255, 255, 255)
  19. 'Image of probability from theory
  20. _PUTIMAGE (0, 0)-(599, 399), _LOADIMAGE("P_SharedBirthday.png", 32)
  21.  
  22. 'LINE (80, 338)-(90, 338) Construction lines to find origin and top-right of graph
  23. 'LINE (85, 333)-(85, 343) to get scale of plotting graph
  24. 'LINE (567, 51)-(567, 61)
  25. 'LINE (562, 56)-(572, 56)
  26.  
  27. LINE (0, 0)-(599, 399), _RGBA32(120, 120, 120, 150), BF
  28.  
  29. PrevX% = 85 + CINT(4.82)
  30. PrevY% = 338
  31. _DELAY 1.5
  32.  
  33. FOR NoFolks% = 2 TO 100
  34.     REDIM Birthdays%(NoFolks% - 1)
  35.     NoSame% = 0
  36.     FOR N% = 1 TO Qty%
  37.         'Randomise birthdays of participants
  38.         FOR M% = 0 TO NoFolks% - 1
  39.             Birthdays%(M%) = Days%(INT(RND * 1461))
  40.         NEXT M%
  41.         'Now find any pairs whose birthdays are the same
  42.         TwoSame%% = False
  43.         I% = 1
  44.         J% = 2
  45.         WHILE NOT TwoSame%% AND I% < NoFolks%
  46.             IF Birthdays%(I% - 1) = Birthdays%(J% - 1) THEN
  47.                 TwoSame%% = True
  48.             ELSE
  49.                 J% = J% + 1
  50.                 IF J% > NoFolks% THEN
  51.                     I% = I% + 1
  52.                     J% = I% + 1
  53.                 END IF
  54.             END IF
  55.         WEND
  56.         IF TwoSame%% THEN NoSame% = NoSame% + 1
  57.     NEXT N%
  58.     'Print probabilities onto theoretical graph
  59.     LINE (PrevX%, PrevY%)-(85 + CINT(4.82 * NoFolks%), CINT(338 - 2.82 * NoSame% * 100 / Qty%)) ', _RGB32(255, 255, 0)
  60.     PrevX% = 85 + CINT(4.82 * NoFolks%)
  61.     PrevY% = CINT(338 - 2.82 * NoSame% * 100 / Qty%)
  62.     _DELAY 0.1
  63. NEXT NoFolks%
  64.  
  65.     _LIMIT 30
  66.  
P_SharedBirthday.png
* P_SharedBirthday.png (Filesize: 24.01 KB, Dimensions: 540x305, Views: 281)
« Last Edit: April 28, 2020, 12:39:07 pm by Qwerkey »

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Same Birthday Within A Group
« Reply #1 on: April 28, 2020, 07:32:41 am »
I like it. Search the attached for "birthday".
* Probability and Statistics.pdf (Filesize: 265.05 KB, Downloads: 212)
You're not done when it works, you're done when it's right.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Same Birthday Within A Group
« Reply #2 on: April 28, 2020, 12:07:28 pm »
Yeah I like seeing math ideas demo'd in QB64.

For convenience when images are required, I recommend and request putting an image and source in zip or 7z so one gets everything one needs in single download.

Instead of using a line, this will show your data points that contrast better with theoretic graph.
Code: QB64: [Select]
  1.     'Print prabilities onto theoretical graph
  2.     CIRCLE (85 + CINT(4.82 * NoFolks%), CINT(338 - 2.82 * NoSame% * 100 / Qty%)), 2, &HFFFFFFFF
  3.  
« Last Edit: April 28, 2020, 12:16:13 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Same Birthday Within A Group
« Reply #3 on: April 28, 2020, 01:29:18 pm »
Here is an alternate approach: https://www.qb64.org/forum/index.php?topic=2507.msg117590#msg117590

using sort to stack duplicates in a sorted array.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Same Birthday Within A Group
« Reply #4 on: April 28, 2020, 05:40:26 pm »
Explaining it really isn't that difficult.  Let's say we have 60 people in a room.  Random distribution says that our average is 5 birthdays each month.

In any given month, let's use 30 days for the day of the month.  Our first birthday can be on any day.  The second birthday has a 1 in 30 chance of being on that same day.  The 3rd birthday has a 1 in 30 chance of being on the same day.  The 4th birthday has a 1 in 30 chance of being on the same day.  The 5th birthday has a 1 in 30 chance of being on the same day.

There's a 4 in 30 chance that somebody has the same birthday as the first guy.

With 5 guys, there's still the chance that the other 4 have birthdays on the same day, even if they don't share them with the first guy.  I'm thinking the chance would be 4/30 + 3/30 + 2/30 + 1/30 for a total 10/30, or 1 in 3 chance of two people sharing the same birthday, in the first month...

With 12 months, you have 12 * 1 in 3 chances that out of a group of 60, some two people share birthdays -- and if that's not 100%, it's close enough to it to call it that!  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!