Help Please: Cool Mathematical Probability
Hi there,
My assignment is to prove that in a group of 30 people, at least 50% have the same birthdays. And in a group of 50 people, 70% have the same birthdays.
I have to use Math.random()*365 for every day of the year and store the values in an array of 30 and 50. I have to check of how many times each array has the same numbers. This program has to run 100,000 times
When you compile and run the program, this is what you should get:
50% 30 people
70% 50 people
This is all I have so far, but I'm stuck and don't know how to check the arrays.
import jpb.*;
public class Project5
{
public static void main (String []args)
{
int[] a = new int[30];
int[] b = new int[50];
int ran1 = 0;
for(int i=0; i<100000; i++)
{
for(int cnt=0; cnt<30; cnt++)
{
ran1 = (int) Math.random()*365;
a[cnt]= ran1;
}
}
}
}
[1023 byte] By [
caritouf] at [2007-9-26 1:23:45]

First of all, thinking in common sense in 30 people it's irrational that 50% of them would have same birthdays...
do you mean that in that 50% there is someone else also birth at the same day?
and your doing it the wrong way... try this:
int days[]=new int[365];
for (int i=0;i<30;i++)
{
days[(int) Math.random()*365]++;
}
int solobirthdays=0;
int manybirthdays=0;
for (int i=0;i<365;i++)
{
if (days=1) solobirthdays++;
if (days>1)
{
manybirthdays+=days;
}
}
System.out.println("Precent: "+manybirthdays/(manybirthdays+solobirthdays));
seems taht my code isn't working hold on a minute...
now i got it... it your (int) Math.random()*365 that return allways 365 because casts allways has higher priority than arithmetics. I copied it to my code... it should be (int)(Math.Random*365)... anyway now it seems to work but I still don't get your point... in 30 people there were 13% of people who had same birthday atleast with one other... 50% isn't really possible?
Never mind, computer simulations like that don't prove anything. But perhaps that was just a misstatement of the problem.
Hi there:
First of all thank you for responding!!!!
I tried your program but it didn't work. I received these error messages:
found: int []
required: boolean
if (days=1) solobirthdays ++;
^
Project.5.java: 28: operator >cannot be applied to int, int []
if (days >1)
^
Project.5.java: 30: operator + cannot be applied to int, int[]
manybirthdays+=days;
^
Well, I also made a mistake when I gave you my program.
Here is what my professor wanted:
Write mathematical modeling program to prove that 30 people at random, 70% probability same birthday. And for 50 people probability 97% same birthday.
Generate 100,000 groups of 30
Add a counter when checking the array
Divide counter by 100,000
Find no 2 numbers that match add 0
Find 2 that match add the counter
use==> Math.random int between 1-366.
I hope this help a little. I would greatly appreciate your assistance on this matter.
Thank you in advance
^
days is an array, but you left off the [ i ] to get an element of the array. The problem is that the person posted code with [ i ] in it without using the [ code ] tags. Even inside the [ code ] tags, [ i ] becomes < i >.
caritouf,
I'm sure this is what you meant, but I believe you're trying to show that, in a group of 30 people, there's a 50% chance that there are at least two people who share a birthday...
by the way, in your code, do you mean:
if (days=1) ...
or
if (days == 1) ...
?
Have you any common sense or are you programmed just for 2 weeks...errors on code are type-errors from me...for example its days==1 and days>1I still don't get how 70 people can all have same birthday in probability of 97%. Well, never mind...
>int days[]=new int[365];
> for (int i=0;i<30;i++)
> {
> days[(int) Math.random()*365]++;
> }
This doesn't make any sense at all.
This is say declare or set and array element at a random element between 1 and 356 and increment it. Ignore this code.
Maybe this will help
import jpb.*;
import java.lang.Double;
public class Project5 {
public static void main (String []args) {
int birthdays[30];
Double randomDay;
for(int i=0; i<100000; i++) {
for(int cnt=0; cnt < 30; cnt++) {
randomDay = new Double(Math.random()*365);
birthdays[cnt]= randomDay.intValue();
}
for(int i=0; cnt < 30; cnt++) {
for(int cnt = 0; cnt < 30; cnt++) {
if (birthdays[i] == birthdays[cnt]) {
//two people have same birthday
}
}
}
}
}
}
P.S. I think that the question is that in a group of 30 people there is a 70% chance two of them have the same birthday. I'm not sure this is valid but it's got to make more sense than 50% of them have the same birthday. From you experience does that make any sense. You sit in a class room and half the people have the same birthday?
Sorry, there is a problem with my post. The second i declarationg needs a different variable name. it will not compile the way I posted it.
THe code I posted is very rough and actually sucks pretty bad. It should get the job done but is definitely very ineffecient.
Well, one the one hand it's a simple probability problem. On the other hand, my statistics is REAL weak. Fortunately, my web skills are high, thus turning up:
http://www.studyworksonline.com/cda/content/explorations/0,1035,NAV2-76_SEP949,00.html
Anyway, here's my code. I know it's homework, but it was quick and painless (save for the results which didn't match the original posters, but they match the web site above, so "Works for me!").
Enjoy!
Make sure you cite me as a reference when you turn it in!
import java.util.Arrays;
import java.util.Random;
public class BDay
{
Random r;
public BDay()
{
r = new Random();
}
public boolean matchingBDays(int numberOfPeople)
{
int bdayList[] = new int[numberOfPeople];
int i;
boolean result = false;
for(i = 0; i < numberOfPeople; i++) {
bdayList[i] = r.nextInt(365);
}
Arrays.sort(bdayList);
for(i = 1; i < numberOfPeople; i++) {
if (bdayList[i] == bdayList[i - 1]) {
result = true;
break;
}
}
return result;
}
public float percentMatching(int iterations, int numberOfPeople)
{
float successes = 0;
float result;
int i;
for(i = 0; i < iterations; i++) {
if (matchingBDays(numberOfPeople)) {
successes++;
}
}
result = successes/(float)iterations;
return result * 100;
}
public static void main(String args[])
{
BDay b = new BDay();
System.out.println("100,000 iterations with 25 people:" +
b.percentMatching(100000, 25));
System.out.println("100,000 iterations with 60 people:" +
b.percentMatching(100000, 60));
}
}
So what was the result? Are we all born on the same day? ;)