Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help! Birthday Paradox Simulation I c++ The birthday paradox, states that given a group of 23 (or more) randomly chosen people, the probability is

Please help! Birthday Paradox Simulation I c++ The birthday paradox, states that given a group of 23 (or more) randomly chosen people, the probability is more than 50% that at least two of them will have the same birthday. Your task is to use simulation to corroborate the mathematical analysis. Algorithm:Include the following libraries: ,,,,.Input (and echo to the output):the number of times to run the simulationa non-negative integer seed value to be used as the input parameter for srand().Repeat the following simulation the number of times specified by the input:Initialize a 365 (no leap day) element Boolean array to all false.Declare and initialize a 365 element integer frequency array.Randomly mark birthdays (true) in the 365 element Boolean array until there's a duplicate.Every time there's not a duplicate birthday, increment the frequency array element whose index is the current number of unmatched birthdays.When there is a duplicate, break out of the loop.When finished, store, in a new (double) array, the ratio of each value in the frequency array to the total number of runs.

This value will be a the simulated probability of not-matching for that many birthdays.In another array of the same size and type, store the theoretical probability of not-matching. ( N = number of birthdays not-matching) computed via the relations:The probability that 0 or 1 birthday won't match is 1.The probability that 2 birthdays won't match is (1 - 1/365) = (1 - ({N=2} - 1)/365) = P(2).The probability that 3 birthdays won't match is (1 - 1/365)*(1 - 2/365) = P(2)*(1 - ({N=3} -1)/365).The probability that N > 0 birthdays won't match is P(N - 1)*(1 - (N -1)/365).output a table where for each row:column 1 is the number of birthdays not-matching (starting with 2),column 2 is the respective theoretical probabilitycolumn 3 is the respective simulated probabilitycolumn 4 is the respective absolute error {theoretical - simulated}column 5 is the respective relative error {fabs((theoretical - simulated) / theoretical)}Can you duplicate the instructors output? (You'll need to use the following I/O manipulators: scientific, setw(), setprecision())Answer the following questions:What happens to the magnitude of the errors when you significantly increase the number of runs?Run the simulations again using different random seeds. What happens? Does your simulation verify that the smallest number of people for which there is a greater than 50% chance of having at least one common birthday is 23?How would you modify your program to simulate the probabilities that M > 2 birthdays would-not match?

Use the following function to compute a random integer in the interval [0, bound): int irand2bound(int bound) { return static_cast( rand() / (RAND_MAX + 1.0) * bound ); }

output looks like

Enter the number of times to run the birthday match simulation: 1234567 The simulation will be run 1234567 times. Enter a positive integer for the random seed: 123 The random seed is 123. Theoretical vs. Computed probability of no birthday matches for a set of people: _______________________________________________________________________________ People Theoretical Computed Error Rel. Err. _______________________________________________________________________________ 2 9.973e-01 9.971e-01 1.196e-04 1.199e-04 3 9.918e-01 9.916e-01 1.591e-04 1.604e-04 4 9.836e-01 9.835e-01 1.057e-04 1.075e-04 5 9.729e-01 9.730e-01 -8.641e-05 8.882e-05 6 9.595e-01 9.595e-01 6.766e-06 7.051e-06 7 9.438e-01 9.439e-01 -1.108e-04 1.174e-04 8 9.257e-01 9.259e-01 -2.494e-04 2.694e-04 9 9.054e-01 9.057e-01 -3.462e-04 3.824e-04 10 8.831e-01 8.834e-01 -3.097e-04 3.507e-04 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Programming With Visual Basic .NET

Authors: Carsten Thomsen

2nd Edition

1590590325, 978-1590590324

Students also viewed these Databases questions