Results 1 to 8 of 8
  1. #1
    fungui is offline Member
    Join Date
    May 2013
    Posts
    5
    Rep Power
    0

    Default How to pick a random string from a list of strings?

    I am making a random sentence generator, but first I need to find out how to pick a random string from a list.
    How can I do this?

    Thanks

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default Re: How to pick a random string from a list of strings?

    What have you tried?

    Do you know how to generate a random number within a range?

    Do you know how to get the index of a List?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,250
    Rep Power
    19

    Default Re: How to pick a random string from a list of strings?

    Where exactly are you stuck?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    fungui is offline Member
    Join Date
    May 2013
    Posts
    5
    Rep Power
    0

    Default Re: How to pick a random string from a list of strings?

    I know how to use randoms, but not with strings. I simply have no idea how to start.

  5. #5
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: How to pick a random string from a list of strings?

    You can generate a random number that is bounded between 2 values. Does that hint help?

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,250
    Rep Power
    19

    Default Re: How to pick a random string from a list of strings?

    Quote Originally Posted by fungui View Post
    I know how to use randoms, but not with strings. I simply have no idea how to start.
    OK. Do you know how to get a String from a list of Strings?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    fungui is offline Member
    Join Date
    May 2013
    Posts
    5
    Rep Power
    0

    Default Re: How to pick a random string from a list of strings?

    I don't no much about strings. Do I make a void containing a list of strings?
    Sorry, I'm new to java.

  8. #8
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: How to pick a random string from a list of strings?

    Take a look at the methods defined in the List interface. If you wanted to get an item from a List, which of those methods would you use? Don't think about the random part yet. Just find the method that you would use to get an item from a list. That is step 1. Here is the link to the JavaDoc:

    http://docs.oracle.com/javase/6/docs...util/List.html
    Last edited by sehudson; 08-21-2013 at 03:06 PM.

Similar Threads

  1. Replies: 1
    Last Post: 03-09-2013, 12:13 PM
  2. Random Strings
    By tgwinford in forum New To Java
    Replies: 2
    Last Post: 03-18-2012, 07:04 PM
  3. Pick a random value from array?
    By RattleSnake in forum New To Java
    Replies: 2
    Last Post: 04-26-2011, 05:36 AM
  4. Pick random array index and change the element value
    By Space Ranger in forum New To Java
    Replies: 8
    Last Post: 03-29-2011, 10:18 PM
  5. Random Strings
    By Jimmy07 in forum New To Java
    Replies: 9
    Last Post: 01-22-2011, 06:47 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
  •