Example 0.5: Compute the probability that of k individuals, no two will have the same birthday; also find the probability that there will be at least 1 match. Assume that each individual, independently of all others, is equally to have a birthday on any one of 365 days in a non-leapyear. Use direct calculations as well as one that uses a linear approximation of the logarithm near 0.
Problem 0.5: Do the analogous problem to Example 0.5 using the 12 astrological signs in place of the 365 days of the year.

Object, Property and settings:
Object Property Setting Form Name frmBirthday BackColor &H00C0C0C0& Caption "Birthday Problem Calculations" Label Name Label1 Alignment 2 'Center BackColor &H00C0C000& Caption "Assume each of 365 days are equally likely for a birthday.:" TabIndex 0 Label Name Label2 Alignment 1 'Right Justify Caption "Number of individuals:" TabIndex 1 TextBox Name txtIndividuals TabIndex 2 Text "23" Label Name Label3 Alignment 1 'Right Justify Caption "The Probability of No Matches:" TabIndex 3 Label Name lblProbNoMatch BorderStyle 1 'Fixed Single TabIndex 4 Label Name Label4 Alignment 1 'Right Justify Caption "Approximation of the Probability of No Matches:" TabIndex 5 Label Name lblApxProbability BorderStyle 1 'Fixed Single TabIndex 6 Label Name Label6 Alignment 1 'Right Justify Caption "The Probability of ar Least 1 Match:" TabIndex 7 Label Name lblProbAtLeastOneMatch BorderStyle 1 'Fixed Single TabIndex 8 Label Name Label8 Alignment 1 'Right Justify Caption "Approximation of the Probability of At Least 1 Match:" TabIndex 9 Label Name lblApxProbAtLeastOneMatch BorderStyle 1 'Fixed Single TabIndex 10 CommandButton Name cmdCompute Caption "&Compute" Default -1 'True TabIndex 11 Menu Name mnuFile Caption "&File" Menu mnuExit Caption "E&xit" Shortcut ^X
Event Procedures:
' In (Declarations) section of (General)
' Programmer: R. Berman
' Date: May, 1996
' Project: Birthday Problem Calculations
' Description: Do calculations for the exact
' and approximate values (based
' on a linear approximation of
' a logarithm) for the probability
' that k individuals have no matches
' of birthdays.
' Version: Microsoft Visual Basic 4.00
' Require Explicit Declaration of Variables
Option Explicit
' The following conventions are used for the
' declarations of constants and variables.
' "m" preceding an underscore means module level.
' "i" preceding an underscore means integer datatype.
' "s" preceding an underscore means single-precision.
' We also follow the convention that named constants
' are in upper case letters except for the datatype
' and scope identifiers preceding the first underscore.
' Declare a module-level constant of integer datatype
' equal to 365 for the number of days in a non-leapyear.
Const mi_DAYSINYEAR As Integer = 365
' Declare a module-level variable of integer datatype
' for the number of individuals.
Dim mi_Individuals As Integer
Private Sub cmdCompute_Click()
' Declare an index variable for multiplication
' of factors. Recall that the "i" preceding the
' underscore signifies an integer datatype.
Dim i_Index As Integer
' Declare a variable for the the probability.
' Recall that the "s" preceding the underscore
' signifies the single precision datatype.
Dim s_Probability As Single
' Assign value of mi_Individuals from the
' txtIndividuals TextBox. Use the "Val"
' function to convert the contents of the
' TextBox to a numeric value to avoid runtime
' errors.
mi_Individuals = Val(txtIndividuals.Text)
' Initialize the probability to 1.
s_Probability = 1
' Compute the probability by taking a product
' of the mi_Indivuals as required by the formula.
For i_Index = 1 To mi_Individuals
' The line continuation character "_"
' is used at the end of the following line.
s_Probability = s_Probability * _
(mi_DAYSINYEAR - i_Index + 1) / mi_DAYSINYEAR
Next i_Index
' Assign the computed value to the output Label
' lblProbNoMatch formatting the result with 4
' decimal places.
lblProbNoMatch.Caption = Format$(s_Probability, "0.0000")
' Similarly assign the difference of the preceding
' number from 1 to the Label lblProbAtLeastOneMatch
lblProbAtLeastOneMatch = Format$(1 - s_Probability, "0.0000")
' Assign the approximate probability based on a
' linear approximation to the logarithm,
' formatting result with 4 decimal places.
lblApxProbability.Caption = _
Format$(Exp(-(mi_Individuals ^ 2 - mi_Individuals) _
/ (2 * mi_DAYSINYEAR)), "0.0000")
' Similarly, assign the difference of the preceding number
' from 1 to the Label lblProbAtLeastOneMatch
lblApxProbAtLeastOneMatch = _
Format$(1 - Exp(-(mi_Individuals ^ 2 - mi_Individuals) _
/ (2 * mi_DAYSINYEAR)), "0.0000")
End Sub
Private Sub Form_Load()
' Center the form in the screen.
frmBirthday.Top = (Screen.Height - frmBirthday.Height) / 2
frmBirthday.Left = (Screen.Width - frmBirthday.Width) / 2
End Sub
Private Sub mnuExit_Click()
' This is the menu choice for ending the program.
End
End Sub


