Results 1 to 3 of 3
  1. #1
    tronez is offline Member
    Join Date
    Dec 2011
    Posts
    1
    Rep Power
    0

    Default Duplicate Birthday's program

    Hi

    Im very new to java and trying to make a simple java program which will work out the chances of two people sharing the same birthday in say a class of 40 people. Using the monte Carlo method the above senerio will be simulated 10,000 times. Below is the code I have already written

    Java Code:
    public class birthday
    {
        public static void main(String[] args)
        {
            long experiments = 10000;
            long bdaycount = 0;
            int bday1, bday2;
            
         
    for(long i = 0; i < experiments; i++) {
            
            
        for(int j = 0; j < 20; j++) {
            
            
           bday1 = probability(); 
           bday2 = probability();
            
            if(bday1 == bday2) {
                bdaycount++;
                
                break;
                
            }
               
        }
    }
    
        System.out.println("prob is: " + ((double)bdaycount / (double)experiments));
    
    }
        public static int probability()
        {
            double x = Math.random();
            x = 1.0 + (x * 365);
            int outcome = (int)Math.floor(x);
            
            return outcome;
            
        }
        
        
    }
    But I quickly figured at that all this is doing is getting two birthdays at a time and comparing those instead of the entire class of 40. I thought that it may be possible to store the values in an array but im not sure. So i made the following array and a for loop to generate the different random bithdays, however i now dont no how I can check this array to see if 2 or more of the same values are present.


    Java Code:
    int myarray[] = new int[40];
            
            for(int i = 0; i < myarray.length; i++){
                myarray[i] = (int)(Math.random() * 365);
                
            }


    Any help will be appreciated.

    Thanks

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Duplicate Birthday's program

    Check if the zero-th entry is equal to any entry after that, check if entry 1 is equal to entry 2->. etc etc. A double (nested) loop should do it.

    Java Code:
    for(each person in array) {
        check for(everyone after them in the array)
    }
    But if you spend a bit more space you get back a lot of speed. Make your array 365 long, not 40. Then for each person in the class add one at the position of their birthday. Now all you have to do is search the "calendar" array for any entry >1. You won't know who the birthday pair are, but that doesn't matter for your problem.

    [edit] thinking a bit, you don't even have to do this much. In particular you don't need to assign everyone a birthday: just assign birthdays and check them against the calendar array. You can stop as soon as you've found a person with a birthday whose slot is already taken. (now the array can be boolean[365])
    Last edited by pbrockway2; 12-05-2011 at 02:24 AM.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

Similar Threads

  1. How do you duplicate a window?
    By Baldie in forum AWT / Swing
    Replies: 6
    Last Post: 06-10-2011, 08:19 AM
  2. Birthday Party Code
    By tonyfingures in forum New To Java
    Replies: 10
    Last Post: 09-16-2010, 04:24 AM
  3. Duplicate table
    By anilkumar_vist in forum New To Java
    Replies: 3
    Last Post: 01-09-2010, 12:17 PM
  4. Duplicate XML decleration
    By gyl2009 in forum XML
    Replies: 0
    Last Post: 03-11-2009, 06:13 PM
  5. using loop to find duplicate
    By gwithey in forum New To Java
    Replies: 7
    Last Post: 03-06-2009, 02:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •