# [SOLVED] Get a random String

• 04-05-2008, 09:26 PM
LeoCoderIV
[SOLVED] Get a random String
Hi,
i am having trouble getting a random string.
I have gotten the String from the text file and put it into an array but now i keep just getting the first line whenever i try to get a random String (which is a single line) from this array.
At the moment my code is:
Code:

```      public String[] generateRandomQuestion()       {       Random randomGenerator = new Random();             System.out.println("");       System.out.println("Your Question is  :  ");       System.out.println("");             for(int q =1;q<2; ++q)       {           System.out.println( Quest[q] );           System.out.println("");                   }         return Quest;     }               }```
i can see why i'm not getting a random because i havent actually asked for one but i am not sure how to do so??

Any help would be much appreciated

Thanks :)
• 04-06-2008, 11:41 AM
CaptainMorgan
Leo, as I wrote about in your introduction, please be articulate your words in full and appropriate English, using abbreviations such as you've done is not guaranteed to be understood by all members reading your post. Writing out full English, is not only appropriate for efficient communication, but increases your chances of being understood.

Please use [ code] [ /code] tags when posting code- use the posting "preview" to verify that you've properly used the tags.

Since you've shown with two posts that you haven't completely recognized the rules of road for being a member here, I must require you to read the FAQ before you post again.

Now that that's out of the way, which could have been avoided very easily... to answer your question about using random number generators, look into using nextInt. An example run, with the code above might be:
Code:

`int rand = randomGenerator.nextInt(20);`
this gives you a random number between 0 and 20. A perusal of Random in the API and you can come up with many ways of doing it. Make an attempt at your custom way of doing it and we can critique or correct you on it.
• 04-06-2008, 10:14 PM
LeoCoderIV
Sorry bad habits i guess :) I will work on it

Thanks for the help i will try it out and get back to you if i need to

Thanks again
• 04-06-2008, 10:44 PM
LeoCoderIV
ok, I tried it out but I dont think I understand fully what i need to do.

I tried:

Code:

`  int rand = randomGenerator.nextInt(Quest.length);`

However, it doesnt make any effect on the output. Could it be that i dont have the correct variables? or i am i on the wrong tracks :o
• 04-07-2008, 03:28 AM
CaptainMorgan
Leo, thank you for your understanding regarding the community rules. It's not something I have great joy for but it's something I committed to upon becoming a moderator. I do without a doubt enjoy assisting members with code specific issues.

I wrote a little example that has relations to your initial code. You can see the many uses of the simple line generating a random number. Remember, the parameter given to nextInt is an inclusive number in that it includes 0 up to and excluding that number. So in this case below, someone will correct me if I'm wrong.. it includes numbers 0-9. Give this a study and post any questions you may have. There's certainly more efficient ways to do this I'm sure, but I think it's sufficient for now. If you have improvements definitely post them if you want.

Code:

```import java.util.Random; public class Question {   String[] questions = new String[10];     public Question() {     // eight prefixes to be chosen at random     String [] prefix =       {"What", "How", "When", "Why", "Are", "Do", "Then", "Like"};     // eight basic, first category words to be chosen at random     String [] wordsOne =       {"time", "day", "cool", "hungry", "is", "can't", "you", "enjoy"};     // eight basic, second category words to be chosen at random     String [] wordsTwo =       {"who", "awesome", "okay", "really", "turtle", "move", "look", "life"};     // eight basic, third category words to be chosen at random     String [] wordsThr =       {"money", "cheese", "run", "couch", "concept", "it", "cat", "factor"};     // only one suffix     String suffix = "?";         Random rand = new Random();     // choose random prefixes and words to make randomly made questions     for (int i = 0; i < questions.length; ++i) {       questions[i] =         prefix[rand.nextInt(8)] + " "           + wordsOne[rand.nextInt(8)] + " "             + wordsTwo[rand.nextInt(8)] + " "               + wordsThr[rand.nextInt(8)] + suffix;          }   }       public String[] generateRandomQuestion() {     Random randomGen = new Random();            System.out.println("\n--Your random question is: \n" +             questions[randomGen.nextInt(10)]);         return questions;          }     public String[] generateRandomQuestions() {     Random randomGen = new Random();            System.out.println("\n--Your random questions are: ");       for (int q = 0; q < questions.length * 2; ++q) {       System.out.println(questions[randomGen.nextInt(10)]);     }         return questions;          }     public static void main(String[] args) {     Question q = new Question();     q.generateRandomQuestion();     q.generateRandomQuestions();   } }```
Sample output shows this:
Quote:

Are hungry who cat?

Then hungry okay cat?
Are hungry life run?
Are day who concept?
When you look run?
When you look run?
Are day who concept?
When you look run?
Why cool okay run?
Are hungry life run?
What you look cat?
Are day who concept?
Do is awesome money?
Then hungry okay cat?
Are day who concept?
Are hungry who cat?
Then hungry okay cat?
Are day who concept?
Do is awesome money?
Why cool okay run?
Then hungry okay cat?
Another change from the code, highlighted below:
Code:

```...   String[] questions = [B]new String[20];[/B] ...   public String[] generateRandomQuestions() {     Random randomGen = new Random();            System.out.println("\n--Your random questions are: ");       for (int q = 0; q < [B]questions.length[/B]; ++q) { [B]      System.out.println(questions[q]); [/B]     }         return questions;          } ...```
shows this for output, note the difference->instead of choosing random strings- outputs all randomly-made strings:
Quote:

How enjoy move factor?

Do time turtle concept?
Do enjoy look money?
How enjoy move factor?
When you who factor?
Like can't look cat?
Why you life it?
Then can't okay it?
Do cool awesome money?
Are enjoy life it?
Do day look cat?
When hungry who cheese?
How enjoy look money?
Do day move cat?
Like you who cheese?
Are time okay money?
Then hungry okay money?
When time turtle concept?
Are day turtle it?
What can't awesome concept?
Then can't look it?
• 04-07-2008, 03:44 PM
LeoCoderIV
Thats great, thanks a lot. It works just how i need it. :D
• 04-07-2008, 03:58 PM
DonCash