Question

In: Computer Science

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 between 0 and n−1.n−1.
  • Have that person enter the room.
  • If that person shares a birthday with someone else in the room, stop; otherwise repeat.

In each experiment, count the number of people that enter the room. Print a table that summarizes the results (the count i, the number of times that exactly i people enter the room, and the fraction of times that i or fewer people enter the room) for each possible value of i from 1 until the fraction reaches (or exceeds) 50%.

Submission. Submit a .zip file containing DiscreteDistribution.java, ThueMorse.java, Birthday.java, and Minesweeper.java. You may not call library functions except those in the java.lang (such as Integer.parseInt() and Math.sqrt()). Use only Java features that have already been introduced in the course (e.g., loops and arrays, but not functions).

Solutions

Expert Solution

Solution :

Following is the fully commented code for the problem :

import java.util.*;
class Main {
    public static void main(String[] args) {
        Scanner sc= new Scanner(System.in); //System.in is a standard input stream.
        System.out.println("Enter no. of days : ");
        int n= sc.nextInt(); //number of days
        System.out.println("Enter no. of trials : ");
        int trials = sc.nextInt();  //number of trials
        boolean[] birthdays = new boolean[n+2];
        int[] times = new int[n + 2]; //number of times i people entered the room
        int r;

        for (int t = 1; t <= trials; t++) {

            for (int k = 0; k < n; k++) { //reset birthday
                birthdays[k] = false;
            }

            for (int i = 1; i <=n; i++) { //number of times

                r = (int) (Math.random() * (n - 1)); //random birthday

                if (birthdays[r] == false) {
                    birthdays[r] = true;
                    continue;
                }

                else if (birthdays[r] == true) {
                    times[i]++; //number of times i people entered the room + 1
                    break;
                }

            }

        }
        int j = 1;
        //continue till the fraction reaches or exceeds 0.5
        while ((double) times[j] / trials <= 0.5) {
            System.out.print(j + "\t" + times[j] + "\t" + ((double) times[j] / trials));
            j++;
            System.out.println("");

        }
    }
}

Code demo :


Related Solutions

Prove "The Birthday Problem" in this regard, Suppose there are some number of people in a...
Prove "The Birthday Problem" in this regard, Suppose there are some number of people in a room and we need need to consider all possible pairwise combinations of those people to compare their birthdays and look for matches.Prove the probability of the matches.
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...
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).
100 people enter a room that is initially at a temperature of 15 C and pressure...
100 people enter a room that is initially at a temperature of 15 C and pressure of 1bar. Each person losses heat to the air in the room at a constant rate of 300 W at a constant temperature of 37 C. The air mass in the room is 750 Kg, remains constant and its cv = 0.79 kJ/(Kg K), cp = 1.08 kJ/(Kg K) and the gas constant is 0.287 kJ/(Kg K). Air enters the room at a temperature...
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...
Problem 7. Show that in a room full of 20 people there are 2 people who...
Problem 7. Show that in a room full of 20 people there are 2 people who have the same number of friends present. Friendship is symmetric. If A is friends with B then B is friends with A.
Consider n people and suppose that each of them has a birthday that is equally likely...
Consider n people and suppose that each of them has a birthday that is equally likely to be any of the 365 days of the year. Furthermore, assume that their birthdays are independent, and let A be the event that no two of them share the same birthday. Define a “trial” for each of the ?n? pairs of people and say that 2 trial (i, j ), I ̸= j, is a success if persons i and j have the...
This is an extension of the Birthday Problem. Suppose you have 500 Facebook friends. Make the...
This is an extension of the Birthday Problem. Suppose you have 500 Facebook friends. Make the same assumptions here as in the Birthday Problem. a) Write a program in R to estimate the probability that, on at least 1 day during the year, Facebooks tells you three (or more) of your friends shat that birthday. Based on your answer, should you be surprised by this occurrence? b) Write a program in R to estimate the probability that, on at least...
   Task Description Task No. Must Follow    Expected time(days) Reserve the meeting room                 A        
   Task Description Task No. Must Follow    Expected time(days) Reserve the meeting room                 A                 None                                      1 Order the marketing material              B                     A                                        9 Brief the managers                             C                     A                                        2 Send put the customer emails            D                     C                                        3 Burn sample DVDs   E                      C                                        3 Load the new software F                      D, E                                    2 Do a class rehearsal.                           G                     B, F                                    1 Now you do: (A) Using the data from the table above, draw a Gantt chart...
PROBLEM 6. Suppose that the birthdays of different people in a group of n people are...
PROBLEM 6. Suppose that the birthdays of different people in a group of n people are independent, each equally likely to be on the 365 possible days. (Pretend there's no such thing as a leap day.)What's the smallest n so that it's more likely than not that someone among the n people has the same birthday as you? (You're not part of this group.)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT