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 9 of 9

Thread: Looking for duplicate birthdays program

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Looking for duplicate birthdays 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

    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 to birthdays at a time and comparing those instead of the entire class of 40. So my question is how can I compare the whole class and see if there are 2 or more birthdays in which are the same. I thought that it may be possible to store the values in an array but im not sure.

    Any help will be appreciated.

    Thanks


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Looking for duplicate birthdays program

    You were right, it would be best to create the birthdays in an array, probably by using another method that creates an int array size 40, and cycles through each index and gives it probability, then calculate from there in either another method or inside the main loop in the main method.

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Looking for duplicate birthdays program

    Ok ive now created an array and also a loop which gives my my 40 different birthday as showed below.

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

    But now I dont really no how I can check the array for 2 or more of the same values.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Looking for duplicate birthdays program

    Improving the world one idiot at a time!

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Looking for duplicate birthdays program

    Can I not look in other places for help? I'm just trying to get my coding issue resolved quickly.

    If I'm not meant to post on different forums with my questions I apologise.

    Is anybody able to help my further with my initial problem?

    Cheers

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Looking for duplicate birthdays program

    Quote Originally Posted by Tronez View Post
    Can I not look in other places for help? I'm just trying to get my coding issue resolved quickly.

    If I'm not meant to post on different forums with my questions I apologise.
    Recommended reading: http://www.javaprogrammingforums.com...s-posting.html

    Basically, we don't want to waste our time answering questions that might have already been answered elsewhere. We don't know what kind of advice or information has been exchanged already, so why bother? We have hundreds of posts to get to, so why answer the one that might already be solved somewhere else? The least you can do is provide a link between crossposts so we know what has already been discussed.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Looking for duplicate birthdays program

    Sorry about that i genuinely didnt realise, but it wont happen again.

    Relating back to the issue with my current code is anybody able to help and give me some advice

    Thanks

  8. #8
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Looking for duplicate birthdays program

    I'd be very grateful if somebody could give me a hand with my coding issue.

    Ive been trying to do this for a while and still cant get it to work.

    Thanks

  9. #9
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Looking for duplicate birthdays program

    You could use another array of the same size and use it as a 'bucket' and just add to the appropriate bucket each time you check a number

Similar Threads

  1. Removing Duplicate Values From an Array
    By nicsa in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 30th, 2011, 07:55 PM
  2. Duplicate elements in 2 Arraylist
    By tcstcs in forum Collections and Generics
    Replies: 3
    Last Post: April 18th, 2011, 12:56 AM
  3. check duplicate key and value in hashmap
    By starmandell in forum Algorithms & Recursion
    Replies: 1
    Last Post: February 9th, 2011, 04:25 PM
  4. duplicate t:dataTable
    By smackdown90 in forum Web Frameworks
    Replies: 0
    Last Post: August 4th, 2010, 10:35 AM
  5. Eliminating duplicate values
    By Harry_ in forum Collections and Generics
    Replies: 7
    Last Post: November 9th, 2009, 06:35 AM