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

Thread: Picking Random Element from Array

  1. #1
    Junior Member Blackrabbitjack's Avatar
    Join Date
    Mar 2012
    Posts
    16
    My Mood
    Fine
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Picking Random Element from Array

    Not too sure if this is the right place to post this, so if it isn't, I apologize. Still figuring out my way around here.

    Anyways, what is the best way to grab a random element from an array? Truly needs to be as close to random as possible.
    Blackjack


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Picking Random Element from Array

    Use the Random class's method to choose an index value between 0 and the length of the array-1
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Blackrabbitjack (March 21st, 2012)

  4. #3
    Junior Member Blackrabbitjack's Avatar
    Join Date
    Mar 2012
    Posts
    16
    My Mood
    Fine
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Picking Random Element from Array

    Would this be the proper way to use this utility?

    public void randomGen(String[] x) {
            Random rangen = new Random();
            int rand = rangen.nextInt(x.length-1);
     
        }
    Blackjack

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Picking Random Element from Array

    Looks like it might work. Write a loop and print out the returned value a few times to see what you get.
    No need to create a new Random object everytime you want a number. Make rangen a class variable.
    Then the method has one statement so you might as well use rangen inline.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member Blackrabbitjack's Avatar
    Join Date
    Mar 2012
    Posts
    16
    My Mood
    Fine
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Picking Random Element from Array

    public void randomGen(String[] x) {
            int rand = rangen.nextInt(x.length-2);
            JOptionPane.showMessageDialog(null, "Randomly chosen word is: " + x[rand]);
        }

    For anyone interested in this code for later, this definitely works. I have some strange assignment to my array, so I have to sub 2 instead of one, otherwise I'll occasionally get a "null" return due to the last element being, well, null =P. JOptionPane's message dialog can be subbed for a system.out.println, but I find the dialogs much nicer to look at in the long run. Thanks again, Norm, for the guidance!

    EDIT: Good idea on the class variable, btw. Originally wrote it contained to see if it would work properly. Is now resting up above with all my others.
    Last edited by Blackrabbitjack; March 21st, 2012 at 03:10 PM. Reason: More info
    Blackjack

Similar Threads

  1. Subtracting each element from an array
    By Tronez in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 15th, 2011, 04:41 PM
  2. add new element in array
    By allen_k in forum Collections and Generics
    Replies: 3
    Last Post: December 11th, 2011, 03:13 PM
  3. Taking an element out of an array
    By SlckP in forum Collections and Generics
    Replies: 3
    Last Post: May 5th, 2010, 02:26 PM
  4. how to find an element in an array
    By humdinger in forum Collections and Generics
    Replies: 8
    Last Post: April 9th, 2010, 05:22 PM
  5. [SOLVED] theory behind testing each element of an array.
    By etidd in forum Java Theory & Questions
    Replies: 2
    Last Post: February 5th, 2010, 09:04 AM