Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Program to check how many people are born on the same day of the month

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Location
    Mass
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Program to check how many people are born on the same day of the month

    Assuming there are exactly 30 days in a month, I need to write a program that considers if there are K people in a room, what is the likelihood that at least M of them were born on the same day of the month. For example, if two people are both born on the 5th day of the month, they are a match.

    So if a user selected the match chance for M=7 (7 duplicates) and K=90 (90 people), what is the probability that at least 7 of them were born on the same day of the month. The probability should be computed by 10,000 trials.
    I'm just really confused about how to compute this. This is what I have so far:

    import javax.swing.JOptionPane;
     
    public class BDayMTester{
      public static void main(String[] args){
        final int trials = 10000;
        String duplicateCt = JOptionPane.showInputDialog("Enter duplicate count");
        String peopleCt = JOptionPane.showInputDialog("Now enter the number of people in the room");
        int dupCt = Integer.parseInt(duplicateCt);
        int pplCt = Integer.parseInt(peopleCt);
     
        BDayM b = new BDayM(pplCt, dupCt);
        double total = 0.0;
        double s = 0.0;
        for(int p=0; p<trials; p++){
          s = b.runTest();
          total = total + s;
        }
        System.out.println(total);
      }
    }

    public class BDayM{
     
      private int roomFolks;
      private int dupPeople;
      private int match = 0;
     
      public BDayM(int people, int duplicates){
        roomFolks = people;
        dupPeople = duplicates;
      }
     
      public double runTest(){
        boolean[] days = new boolean[30];
        for(int d = 0; d<30; d++){
          days[d] = false;
        }
     
        int ranDay;
        for(int p=0; p<roomFolks; p++){
          ranDay = genDay();
          if(days[ranDay] == true){
            days[ranDay] = true;
            match++;
          }
          else days[ranDay] = true;
        }
        return dupPeople/match;
      }
     
      private int genDay(){
        return((int)(30*Math.random()));
      }
    }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Program to check how many people are born on the same day of the month

    if two people are both born on the 5th day of the month
    Any month or the same month only? For example, does jan 20 'match' feb 20?

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Location
    Mass
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Program to check how many people are born on the same day of the month

    yes, as long as the day is the same, the month doesn't matter

  4. #4
    Junior Member
    Join Date
    Apr 2012
    Location
    Mass
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Program to check how many people are born on the same day of the month

    Sorry to keep posting walls of code, but I think I've almost found the solution. I don't believe I'm getting a correct average from the 10,000 trials though. Here is my new code:

    import javax.swing.JOptionPane;
     
    public class BDayMTester{
      public static void main(String[] args){
        final int trials = 10000;
        String duplicateCt = JOptionPane.showInputDialog("Enter duplicate count");
        String peopleCt = JOptionPane.showInputDialog("Now enter the number of people in the room");
        int dupCt = Integer.parseInt(duplicateCt);
        int pplCt = Integer.parseInt(peopleCt);
     
        BDayM b = new BDayM(pplCt, dupCt);
     
        double average = 0.0;
        double s = 0.0;
        for(int j=0; j<trials; j++){
          s = b.runTest();
          average = average + s;
      }
        System.out.println((double)average/trials);
      }
    }

    public class BDayM{
     
      private int roomFolks;
      private int dupPeople;
      private int match = 0;
      private int s = 0;
     
      public BDayM(int people, int duplicates){
        roomFolks = people;
        dupPeople = duplicates;
      }
     
      public double runTest(){
        int[] count = new int[30];
        for(int j = 0; j<roomFolks; j++){
          s = genDay();
          count[s]++;
        }
        for(int n=0; n<30; n++){
          if(count[n] > 1){
            match++;
          }
      }
        return (double)dupPeople/match;
      }
     
      private int genDay(){
        return((int)(30*Math.random()));
      }
    }

    With an input of 7 matches and 90 people, I'm getting an answer like 2.81458828680363E-4 when it should be something like 0.655565

    EDIT: I think it's because I'm only including if there are 7 matches, and 7 matches alone in the group of people when I should be considering if there are 7 or MORE matches in the group.

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Program to check how many people are born on the same day of the month

    .....
        for(int n=0; n<30; n++){
          if(count[n] > 1){
            match++;
          }
    ......
    What exactly does the above code do? Is that what you meant for it to do?
    Is the return value from the runTest() method a good value each run?
    Try a println on dupPeople, match, and dupPeople/match before the return of runTest() and see what the values look like. Perhaps a fewer number of tests for this...

  6. #6
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Program to check how many people are born on the same day of the month

    Quote Originally Posted by ksahakian21 View Post
    ...
    EDIT: I think it's because I'm only including if there are 7 matches
    It's worse than that...

    Let's go back and look at the problem from a higher point of view.

    Here's the Big Picture for things like your assignment:

    • You define an "experiment." The outcome of the experiment is either "true" or "false."

    • You run the experiment N times, and count the number of successful ("true") outcomes.

    • The probability of "success" is approximately equal to the number of "true" results divided by N.


    In your case "success" is defined to be "true" if there are seven or more people in the room who celebrate birthdays on the same monthday (subject to your assumption that there are exactly 30 days in every month.)

    So, looking at your BDayM class:


    In the BDayM class, create a boolean function. Maybe call it runTest() or some such thing. You are going to run this test a bunch of times and count the number of times it returns "true."

    In general, there will be a certain number of people and a certain "match point" integer value that we will use to define whether the outcome of the test is true or false.

    Using your example numbers of 90 people and a "match point" of 7, here's the way one test of the experiment would run:

    Declare an array of 30 integer counters.
    All counters are initially zero.
     
    Go through a loop that generates 90 random
    numbers with value 0..29.
     
    Each time through the loop, increment the
    counter element corresponding to the
    particular value of the random number.
     
    (You have pretty much done all of the above, right?)
     
     
    Then here's the Good Stuff:
     
    Make another loop.
    Go through the array of counters.  If you find a
    counter whose value is greater than or equal to 7,
    the test is "success," so you return "true" at that
    point.
     
    There is no need to continue counting, and there is
    no purpose of keeping track of the total number
    of matches or anything else.
     
    If, on the other hand, you get through the loop that
    has inspected counter values and have not found any
    one that is greater than or equal to 7, then
    return "false."

    Now, in main():
    Create the BDayM class with a particular number
    of people and a particular value of match point.
     
    Declare an integer that will keep track of the
    number of times the test returns "true."
    Initialize it to zero.
     
    Declare an int that will be the number of
    times that you run the test.  Call it N.
     
    Make a loop that does the following:
     
    Run that test N times.
     
    That is, each time through the loop
    run the test. If it returns "true" increment the
    "success" counter.
     
    Then the probability you are seeking is approximately
    equal to the number of "true" outcomes divided by the
    total number of tests.  (Cast to floating point before
    dividing integers.)


    Cheers,

    Z

  7. #7
    Junior Member
    Join Date
    Apr 2012
    Location
    Mass
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Program to check how many people are born on the same day of the month

    I did eventual figure this out after getting my head wrapped around it.

    import javax.swing.JOptionPane;
     
    public class BDayMTester{
      public static void main(String[] args){
        final int trials = 10000;
        String duplicateCt = JOptionPane.showInputDialog("Enter duplicate count");
        String peopleCt = JOptionPane.showInputDialog("Now enter the number of people in the room");
        int dupCt = Integer.parseInt(duplicateCt);
        int pplCt = Integer.parseInt(peopleCt);
     
        // Creates a new BDayM object with user inputed values
        BDayM b = new BDayM(pplCt, dupCt);
     
        int match = 0;
        // Goes through the set number of trials determining
        // how many true outcomes occur.
        for(int j=0; j<trials; j++){
          if(b.runTest() == true){
            match++;
          }
        }
     
        double toPercent = ((double)match/trials)*100;
     
        System.out.println("Match chance for " + pplCt + " people: looking for " + dupCt + " duplicates");
        System.out.println((double)match/trials);
        System.out.println(toPercent + "%");
      }
    }

    public class BDayM{
     
      private int roomFolks;
      private int dupPeople;
      private int s = 0;
     
      public BDayM(int people, int duplicates){
        roomFolks = people;
        dupPeople = duplicates;
      }
     
      public boolean runTest(){
        int[] count = new int[30];  // array to hold each possible day
        // This for loop generates a random day of the month for each person
        // then applies that day to the counter array which keeps track of
        // how many birthdays are on each day.
        for(int j = 0; j<roomFolks; j++){
          s = genDay();
          count[s]++;
        }
        // This for loop goes through each counter array index and checks if that
        // index meets the condition of being at least the value dupPeople (which
        // is inputed by the user in the BDayTester class).
        for(int n=0; n<30; n++){
          if(count[n] >= dupPeople){
            return true;  // If the condition is met, runTest() returns true
          }
        }
        return false; //otherwise runTest() will retun false
      }
     
      // Calculates a random day 0-29 for use in the runTest() method
      private int genDay(){
        return((int)(30*Math.random()));
      }
    }

Similar Threads

  1. Resetting a Variable on a Day of the Month
    By HarleyRowland in forum Java Theory & Questions
    Replies: 1
    Last Post: October 15th, 2012, 10:36 AM
  2. Java program inputting month name and outputting month number and number of days
    By Brovahkiin501 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 19th, 2012, 09:30 AM
  3. Array month/day/hour/min/sec incomplete java code
    By mightyking in forum Collections and Generics
    Replies: 12
    Last Post: September 18th, 2011, 08:46 PM
  4. Replies: 1
    Last Post: May 14th, 2011, 04:57 PM
  5. Day,Month,Year Concatenation Error
    By srinivasan_253642 in forum JDBC & Databases
    Replies: 1
    Last Post: January 22nd, 2010, 04:40 AM