Quick Sup Forums! Write a Java program that finds, on average...

Quick Sup Forums! Write a Java program that finds, on average, the amount of people you would need to poll before you found one with the same birthday as another person already polled, or this bird will kill you!

Other urls found in this thread:

en.m.wikipedia.org/wiki/Birthday_problem
twitter.com/NSFWRedditGif

The year doesn't matter, just the date.

b-b-b-b-bump

Why is there a ziptie around the birds leg?

WRONG

See my shitty code and laugh. Every time I run it it's [spoiler]24[/spoiler] people.


Random rand = new Random();
ArrayList list = new ArrayList();
Set set = new HashSet();
for (int i = 0; i < 50000; i++) {
int count = 0;
while (true) {
int temp = rand.nextInt(365);
count++;
if (set.contains(temp))
break;
set.add(temp);
}
set.clear();
list.add(count);
}
// System.out.println(list);
int sum = 0;
for (int num : list) {
sum += num;
}
System.out.println(sum / list.size() + " people average");

Oops, no spoilers on Sup Forums. Well whatever.

>sol1
google birthday problem and give solution
>sol2
use pigeonhole principle and give answer
God why am i on this childish shithole? im a software engineer and still browsing this inane site back when i was 12

You don't even need a computer program for that. Its equal to half the days in a year if each day is equally likely to have a birthday on it.

Read the question again.

At the halfway point the probability of getting a person with the same birthday as one already polled flips into favoring a pre polled birthday from a new one. If each day is equally likely that a person was born on it then as the number of trials tends to infinity, the average number of people to find one with the same birthday as one previously polled approaches half the amount of days in a year. Additionally as you approach 365 people polled the probability of getting one with a birthday already seen approaches 1 in 1.

10 PRINT HACK THE PLANET
20 GOTO 10

As an aside note you'll find that in real life the number is actually MUCH MUCH lower than half the number of days in the year owing to the fact that the most people are born in June (I believe I haven't checked it in a long time) and very few people are born in February and January. So once you survey someone born in the most common month your chances of surveying someone with the same birthday as a previously polled person skyrockets.

...

>the average number of people to find one with the same birthday as one previously polled approaches half the amount of days in a year
So you're saying on average it will take 182.5 people before one will have the same birthday as another one. Maybe there's something I'm not getting here but I really don't think this is true.

Good point.

en.m.wikipedia.org/wiki/Birthday_problem
>not knowing about the birthday problem

It is true given an imaginary world where people are equally likely to be born on each day. Check my math.

Where:

a is the number is people polled
b is the number of days of the year polled
c is the probability of a person having the same birthday as another
d is the probability of pulling a person that doesn't have a birthday we have already

c = a/365
d = (365-b)/365

if a >= 365/2 then c >= 1/2

as c approaches 1 b approaches 365 and d approaches 0;

Since we stop trials when we get a duplicate birthday and as c increases we have a higher chance of getting a duplicate we can consider the median of c as the average since c is linear. I'm not sure how else to break this down. Maybe I am making a mistake but i don't see it. It makes perfect sense to me.

Let's start by polling one person, he tells you his birth date

You poll another one, that's a 1/365 chance the birth date will be the same

You poll next one, now there's a 2/365 chance the birth date will match one of the previous ones

Another one, 3/365 chance

Polling just 25 people you already have a (25 * 25/365) / 2 = 85.6% chance that you'll have a match

Ah I see now. I was wrong. Thank you.

so following up on this , you should need to ask ~28 people to have a match if I'm not mistaken

Isn't it a n!/365 chance, though?

I looked it up on wikipedia and it's 23 actually.

My code in should have the "if (set.contains(temp)) break;" and "count++" reversed in order, and it gets 23 if you do switch those lines.

You must take the discrepancy into account, say more people being born 9 months from a vacation like new year etc

What a stupid probability problem

Better or worse than this?