#Percent that two people have a birthday within a day of each other.

9 messages ยท Page 1 of 1 (latest)

barren cloakBOT
#

โŒ› This post has been reserved for your question.

Hey @wintry talon! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

#

Please format your code to make it more readable.
For java, it should look like this:
```โ€‹โ€‹โ€‹`โ€‹java
public void foo() {

}
โ€‹โ€‹โ€‹`โ€‹```

safe spade
#

Your program is an attempt to simulate the birthday paradox, which is the probability that in a group of people, at least two of them share the same birthday. However, there are a few issues in your code:

Array Index Out of Bounds:
In the line if (hasBirthday[d] && hasBirthday[d+1]) break;, you are accessing hasBirthday[d+1] without checking if d+1 is within the bounds of the array. This can lead to an ArrayIndexOutOfBoundsException.
Similarly, in the line if (hasBirthday[d] && hasBirthday[d-1]) break;, you are accessing hasBirthday[d-1] without checking if d-1 is within the bounds of the array.
Incorrect Probability Calculation:
The formula you are using to calculate the percent is not correct. The correct formula to calculate the percentage is (people / trials) * 100. In your case, it should be ((double)people / trials) * 100.
Unnecessary Code:
The block of code if (hasBirthday[d] && hasBirthday[d-1])break; and the subsequent lines are unnecessary and can be removed.
Here's an improved version of your code:

public class BirthdayParadox {

    public static void main(String[] args) {
        if (args.length != 1) {
            System.out.println("Usage: java BirthdayParadox <number_of_days>");
            return;
        }

        int days = Integer.parseInt(args[0]);     // number of days
        int people = 0;                           // total number of people
        int trials = 100000;                      // number of trials

        for (int trial = 0; trial < trials; trial++) {
            boolean[] hasBirthday = new boolean[days];
            while (true) {
                people++;
                int d = (int) (days * Math.random()); // integer between 0 and days-1
                if (d < days - 1 && hasBirthday[d] && hasBirthday[d + 1]) {
                    break; // two people with the same birthday within a day of each other
                }
                hasBirthday[d] = true; // update array
            }
        }

        double probability = ((double) people / trials) * 100;
        System.out.println("Percent that two people have a birthday within a day of each other is: " + probability);
    }
}

This version should address the issues mentioned above. It runs the simulation for a specified number of trials, and then calculates the percentage based on the number of people and trials.

barren cloakBOT
remote sinew
wintry talon
#

Thank you @safe spade , I ran the modified code and keep getting the result 7100, this is the percent. I am pretty sure this is not the correct result

barren cloakBOT
safe spade