Results 1 to 10 of 10
  1. #1
    Urgle is offline Member
    Join Date
    Oct 2008
    Posts
    30
    Rep Power
    0

    Default Problem with randoms

    my program is trying to generate random sentences using arrays. For some reason even though i made seperate arrays when i want to use the same elements it still generates the same words. I am doing the random generator wrong?

    Java Code:
    package RandomGeneratedSenteces;
    
    import java.util.*; 
    import java.util.Random;
    
    public class RandomGeneratedSenteces {
    public static void main (String[] args) { 
    
    int i;
    
    
    String[] articles= {"The", "A", "One", "Some", "Any"};
    String[] noun= {"boy", "girl", "dog", "town", "car"};
    String[] verb= {"drove", "jumped", "ran", "walked", "skipped"};
    String[] preposistion= {"to", "from", "over", "under", "on"};
    String[] articles2= {"the", "a", "one", "some", "any"};
    String[] noun2= {"boy", "girl", "dog", "town", "car"};
        
    Random generater = new Random();
    int selectedElement = generater.nextInt(articles.length);
    
    
    for (i=1; i<=10; i++)
    {String randomSentence= articles[selectedElement] + " " + noun[selectedElement] + " " + verb[selectedElement] + " " + preposistion[selectedElement] + " " + articles2[selectedElement] + " " + noun2[selectedElement] + "." ;
    
    System.out.println ("Sentence: " + randomSentence);
    }
    
    }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    To get different words, you need to change the value of selectedElement every time you use it. Your code only sets it once outside of the loop.

  3. #3
    Urgle is offline Member
    Join Date
    Oct 2008
    Posts
    30
    Rep Power
    0

    Default

    how would I go about doing that?

  4. #4
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    You already did it once with the following line:

    Java Code:
    int selectedElement = generater.nextInt(articles.length);
    You just need to do it more than this one time.

  5. #5
    Urgle is offline Member
    Join Date
    Oct 2008
    Posts
    30
    Rep Power
    0

    Default

    Ok, I fixed that issue, so now how do I fix it so it generates a whole different sentence every time. Because I have it set to output 10 sentences but they are all the same.

    Java Code:
    package RandomGeneratedSenteces;
    
    import java.util.*; 
    import java.util.Random;
    
    public class RandomGeneratedSenteces {
    public static void main (String[] args) { 
    
    int i;
    
    
    String[] articles= {"The", "A", "One", "Some", "Any"};
    String[] noun= {"boy", "girl", "dog", "town", "car"};
    String[] verb= {"drove", "jumped", "ran", "walked", "skipped"};
    String[] preposistion= {"to", "from", "over", "under", "on"};
    String[] articles2= {"the", "a", "one", "some", "any"};
    String[] noun2= {"boy", "girl", "dog", "town", "car"};
        
    Random generater = new Random();
    int SE1 = generater.nextInt(articles.length);
    int SE2 = generater.nextInt(articles.length);
    int SE3 = generater.nextInt(articles.length);
    int SE4 = generater.nextInt(articles.length);
    int SE5 = generater.nextInt(articles.length);
    int SE6 = generater.nextInt(articles.length);
    
    for (i=1; i<=10; i++)
    {String randomSentence= articles[SE1] + " " + noun[SE2] + " " + verb[SE3] + " " + preposistion[SE4] + " " + articles2[SE5] + " " + noun2[SE6] + "." ;
    
    System.out.println ("Sentence: " + randomSentence );
    }

  6. #6
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    by more than once, i meant by putting it in the loop.

  7. #7
    Urgle is offline Member
    Join Date
    Oct 2008
    Posts
    30
    Rep Power
    0

    Default

    hmm I put the generator in the loop and it still gave me all the same sentence.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Something like this he's suggesting.

    Java Code:
            for (i=1; i<=10; i++) {
                int SE1 = generater.nextInt(articles.length);
                int SE2 = generater.nextInt(articles.length);
                int SE3 = generater.nextInt(articles.length);
                int SE4 = generater.nextInt(articles.length);
                int SE5 = generater.nextInt(articles.length);
                int SE6 = generater.nextInt(articles.length);            
                
                String randomSentence= articles[SE1] + " " + noun[SE2] + " " + verb[SE3] + " " + preposistion[SE4] + " " + articles2[SE5] + " " + noun2[SE6] + "." ;
                System.out.println ("Sentence: " + randomSentence );
            }

  9. #9
    Urgle is offline Member
    Join Date
    Oct 2008
    Posts
    30
    Rep Power
    0

    Default

    awsome, thanks guys it works perfect

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    If you doing this just to learn Java and random generating, try to do it with multidimensional arrays. So you'll learn a lot about Java arrays as well. But you must have a design, a perfect one.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •