Question

In: Computer Science

The birthday paradox says that the probability (chance) that two people in a room will have...

The birthday paradox says that the probability (chance) that two people in a room will have the same birthday is more than half as long as n, the number of people in the room, is more than or equal to 23. This property is not really a paradox, but many people find it surprising. Write a Java program that generates 10 sets of 23 valid birthdays (ignore leap years). Check how many times the Birthday Paradox occurs and keep count of it. ONLY using arraylist please in java

Solutions

Expert Solution

Thanks for the question.

Here is the completed code for this problem.  Have used ArrayList only as you requested and not arrays.

Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. 

If you are satisfied with the solution, please rate the answer. 

Thanks
===========================================================================

import java.util.ArrayList;
import java.util.Random;

public class BirthdayParadox {

    public static void main(String[] args) {


        int countOfBParadox = 0;
        Random random = new Random();
        int EXPERIMENTS = 10; // the number of sets we want to perform the experiement
        for (int experiement = 1; experiement <= EXPERIMENTS; experiement++) {

            // create an arraylist of integer
            // there will be 366 indices each representing a day in a year
            // the first index is not considered, that is index 0 is left out
            // index 1 will represent day 1 ie. January 1st, index 364 represent 30th December like wise
            //index 365 represent 31st December like wise
            ArrayList<Integer> birthdays = new ArrayList<Integer>();
            for (int blank = 1; blank <= 366; blank++) birthdays.add(0);
            for (int person = 1; person <= 23; person++) {

                // birthdate can be any day in the 365 days of a year
                int birthDate = random.nextInt(365) + 1;
                // increment the value by 1, so if there was already a birthday
                // previously at this date it will increment to 2
                birthdays.set(birthDate, birthdays.get(birthDate) + 1);
                 // check the value is 2, then the birthday is repeating
                if (birthdays.get(birthDate) == 2) {
                    System.out.println("Same birthday found.");
                    countOfBParadox++;
                    break;
                }
            }
        }

        System.out.println("We have found " + countOfBParadox + " times when the birthday was repeated " +
                " in a set of " + EXPERIMENTS);
        System.out.println("Probability (%): "+countOfBParadox*1.0/EXPERIMENTS);
    }
}

=================================================================


Related Solutions

What's the probability that in a room of k people, exactly two share the same birthday?...
What's the probability that in a room of k people, exactly two share the same birthday? Assume 365 days (no leap year).
What is birthday paradox?Explain with it an example.what is the probability of finding a collision for...
What is birthday paradox?Explain with it an example.what is the probability of finding a collision for an ideal 60 bit hash function?what is the main reason for this probability?
There are 4 people in a room. What’s the probability that there are two people born...
There are 4 people in a room. What’s the probability that there are two people born on the same day of the week? (Assume all birthdays are independent and are uniformly distributed over the seven days of the week.)
The birthday problem considers the probability that two people in a group of a given size...
The birthday problem considers the probability that two people in a group of a given size have the same birth date. We will assume a 365 day year (no leap year birthdays). Code set-up Dobrow 2.28 provides useful R code for simulating the birthday problem. Imagine we want to obtain an empirical estimate of the probability that two people in a class of a given size will have the same birth date. The code trial = sample(1:365, numstudents, replace=TRUE) simulates...
Birthday problem. Suppose that people enter a room one at a time. How people must enter...
Birthday problem. Suppose that people enter a room one at a time. How people must enter until two share a birthday? Counterintuitively, after 23 people enter the room, there is approximately a 50–50 chance that two share a birthday. This phenomenon is known as the birthday problem or birthday paradox. Write a program Birthday.java that takes two integer command-line arguments n and trials and performs the following experiment, trials times: Choose a birthday for the next person, uniformly at random...
There are 4 people in a room, and we are curious whether two of them have...
There are 4 people in a room, and we are curious whether two of them have their birthday in the same month. We record the quadruple that describes the month of birthday for each person. E.g. (Oct, Jan, Jul, Apr) is one possible quadruple /outcome.(Since each month applies to a specific person, (Oct, Jan, Jul, Apr) is not the same as(Jan, Jul, Oct, Apr) and so order is important.) a.How many possible outcomes are there? b.In how many of these...
What is the probability that in a group of three people at least two will have...
What is the probability that in a group of three people at least two will have the same birth month? (Assume that all sequences of three birth months are equally likely.) (b) What is the probability that in a group of n people, n ≤ 12 , at least two will have the same birth month? (c) What is the probability that in a group of n people, n > 12 , at least two will have the same birth...
5 people walk into a room in random order. What is the probability that no person...
5 people walk into a room in random order. What is the probability that no person is in correct alphabetical position? For instance, if it is 4 people A, B, C, and D, and they walk in the order B, D, C, A then person C, having walked in 3rd, did have the correct alphabetical position?
What is the probability that out of a class of 25 people at least two have...
What is the probability that out of a class of 25 people at least two have the same birthday? Assume that each birthday is equally likely and that there are only 365 days on which to be born each year. State you answer as a decimal rounded to six decimal places.
Given a group of four people, find the probability that: (a) at least two have the...
Given a group of four people, find the probability that: (a) at least two have the same birth month (b) at least two have the same birthday Assume each day or month is equally likely. Ignore leap years. [Hint: First calculate the probability that they all have different birthdays. Similar to Q5 but with either 12 or 365 hotels.] Answer to a) should be 0.427 Answer to b) should be 0.00164
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT