見出し画像

【シミュレーション】クラスに何人いれば、誕生日の一致する組が存在するか?

学校の1クラスに何人くらいいれば、誕生日が一致する2人組が存在するのか、シミュレーションしてみました!

問題の概要

問題の要点は以下のようなものです。

問題:
N 人のクラスにおいて、誕生日の一致する2人組が存在する確率は、どの程度か?

直感に反するという意味で、「誕生日のパラドックス」とも呼ばれる問題ですね。
下の記事が詳しいのでご参照ください。

シミュレーション

エクセルVBAを用いてシミュレートした結果が下の表とグラフです。

画像1

画像2

結果

理論値と高い精度で一致する結果となりました!

エクセルとVBA全文

今回の計算に使用したエクセルです。

VBAの全文を掲載しておきます。

Option Explicit

#Const REF = False

Sub main()
   Const N As Long = 100000

   Dim row As Long
   Dim personsCount As Long
   Dim i As Long
   Dim j As Long
   Dim randomBirthday As Date
   Dim birthdayStr As String
   Dim count As Long
   
   #If REF Then
       Dim birthdayDictionary As New Scripting.Dictionary
   #Else
       Dim birthdayDictionary As Object
       Set birthdayDictionary = CreateObject("Scripting.Dictionary")
   #End If
   
   row = 3
   Do While Cells(row, 2) <> ""
       personsCount = Cells(row, 2).Value
       count = 0
           
       For i = 1 To N
           For j = 1 To personsCount
               randomBirthday = WorksheetFunction.RandBetween(2, 366)
               birthdayStr = Format(randomBirthday, "mmdd")
               
               If birthdayDictionary.Exists(birthdayStr) Then
                   count = count + 1
                   Exit For
               Else
                   birthdayDictionary.Add birthdayStr, birthdayStr
               End If
           Next j
           
           birthdayDictionary.RemoveAll
       Next i
   
       Cells(row, 3) = count / N
   
       row = row + 1
   Loop
End Sub

―――――記事はここまで―――――
読んでくれて、ありがとうございました!

この記事が気に入ったらサポートをしてみませんか?