'This file is named birthday.vb 'Birthday paradox 'solved by simulated experimentation Imports System Imports Microsoft.VisualBasic Public Module Birthday Sub Main() Const NumExperiments As Integer = 100000 Const myDebug As Boolean = FALSE Dim n, numMatches, i, j, k as Integer Dim birthdays(100) As Integer 'array of n birthdays (numbers between 1 and 365, inclusive) Dim answer As double Dim match As Boolean numMatches = 0 System.Console.Write("Please enter how many people in the group for birthday paradox problem: ") n = System.Console.Readline() System.Console.Writeline("You entered " & n & "...") For i=1 to NumExperiments if myDebug then System.Console.WriteLine("Experiment #" & i) j = 0 match = false 'generate n random birthdays and check for matches so far Do while not(match) And j < n birthdays(j) = int(Rnd() * 365) + 1 if myDebug then System.Console.Writeline("bday #" & j & ": " & birthdays(j)) if j > 0 then 'check all previous birthdays for a match and exit early if found if myDebug then System.Console.WriteLine("Checking for match") for k = j-1 to 0 Step -1 if birthdays(j) = birthdays(k) then match = true if myDebug then System.Console.Writeline("match bdays " & k & " and " & j) exit for end if next k end if if match then exit do j += 1 loop if match then numMatches += 1 Next i answer = (numMatches * 1.0)/ NumExperiments * 100 System.console.writeline("The chance that someone has same birthday is " & answer & " percent.") End Sub End Module