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

Thread: random

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default random

    Hello evreyone


    could anyone please give an simple example on how to select a string by random from a list of strings

    and thank you


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: random

    What class/object is holding the Strings?
    If an array, use one of the Random class or the Math.random() method to generate a number from 0 to the size-1 of the array.

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

    JavaPF (June 3rd, 2010)

  4. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: random

    Norms idea is the best way if you are going to be using an array. Here is a quick example for you:

    public class RandomString {
     
    	/**
    	 * JavaProgrammingForums.com
    	 */
    	public static void main(String[] args) {
     
    		int rand = (int) (Math.random()*4);
     
    		String[] array = new String[4];
     
    		array[0] = "Java";
    		array[1] = "Programming";
    		array[2] = "Forums";
    		array[3] = ".com";
     
    		System.out.println(array[rand]);
     
    	}
     
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Generation of random number using random class
    By JavaPF in forum Java SE API Tutorials
    Replies: 1
    Last Post: December 7th, 2011, 05:46 PM
  2. Random Access File
    By silver_unicorn in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: August 12th, 2009, 01:51 AM
  3. Random Access File
    By silver_unicorn in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: August 10th, 2009, 10:44 PM
  4. Random numbers
    By Pooja Deshpande in forum Java SE APIs
    Replies: 8
    Last Post: June 5th, 2009, 04:36 AM
  5. [SOLVED] Random number method implementation
    By big_c in forum Java Theory & Questions
    Replies: 2
    Last Post: April 15th, 2009, 01:10 PM