-1

I was wondering if there is a way to randomly print strings. I have this code where I have bushes represented by "( )" and rocks represented by ".". Now I want these to print out according to a random int in a different class. However they just print in one straight line. I was wondering if there is an easy way to make them all randomly generate. So instead of looking like this ( ) ( ) ( ) ( ).. they look more like this ( ) . ( ) . ( ) ( ). Thanks a lot for whoever takes the time to respond.

With a final output of

           ^
          /|\
   . (  ) /|\    .   (  )     ( )

Program:

import java.util.ArrayList;

public class ForestRandomizer {

    public static String forestGen()
    {
            String bush = "( )";
            ArrayList<String> bushes = new ArrayList<String>();

            for(int n = 0; Walking.bushesInArea > n; n++)
            {
                bushes.add(bush);
            }

            String rock = ".";
            ArrayList<String> rocks = new ArrayList<String>();

            for(int n = 0; Walking.rocksInArea > n; n++)
            {
                rocks.add(rock);
            }


            System.out.println();
            System.out.println();
            System.out.println();
            for(int i = 0; i < bushes.size(); i++)
            {
                System.out.print(bushes.get(i) + "  ");
            }
            for(int i = 0; i < rocks.size(); i++)
            {
                System.out.print(rocks.get(i) + "  ");
            }
            System.out.println();

        return null;
    }
}
Isaiah Mitchell
  • 75
  • 3
  • 11
  • 2
    Utilize [`Math.random()`](http://stackoverflow.com/questions/7961788/math-random-explained) – Brandon White May 09 '17 at 01:55
  • Generating random values is a very common and basic task. Have you done any research or made any attempt yet? – shmosel May 09 '17 at 01:56
  • 1
    Don't create `bushes` and `rocks` create an `ArrayList` mixing them – Ôrel May 09 '17 at 01:57
  • @J.N. Why does it matter if it's a school assignment? This question matches the sites criteria. –  May 09 '17 at 02:34
  • 2
    @theProgrammer101 - this explains it pretty well: https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions . "Make a good faith attempt to solve the problem yourself first" - He didn't. – J.N. May 09 '17 at 02:37
  • @J.N. He showed code, told us the exact problem, what he got as his output, and what he wanted as his output. His question was pretty exact... –  May 09 '17 at 02:42
  • 1
    @theProgrammer101 - Nope, he asking for a way to *randomly* generate them, without showing any attempt to do so on his own. – J.N. May 09 '17 at 02:43
  • He doesn't know how to, that's what this site is for. If he knew how to, he wouldn't have asked this question. The OP must just not know about the `Math.random` or `Random` methods. This is why we tell him what they are. He has shown effort by producing his code, so I don't see what else is necessary in his post. –  May 09 '17 at 02:52
  • 1
    An simple attempts from OPs would have give him [this duplicate](http://stackoverflow.com/questions/363681/how-to-generate-random-integers-within-a-specific-range-in-java) – AxelH May 09 '17 at 05:53
  • @J.N. i did actually – Isaiah Mitchell May 09 '17 at 16:34
  • @BrandonWhite i tried to use the math.random – Isaiah Mitchell May 09 '17 at 16:35
  • @theProgrammer101 i know about the math.random code but I could not see a way to use that to scramble the order. Thank you all who helped me. – Isaiah Mitchell May 09 '17 at 16:47
  • @AxelH im not trying to generate a random number im trying to rearrange and array in a random order – Isaiah Mitchell May 09 '17 at 16:48
  • @IsaiahMitchell And how do you think you will get a random order in an array ? With a random index may ? Which is a random number with a range `[0... array.length]` – AxelH May 10 '17 at 05:30
  • @AxelH i would run into out of bounds errors and i dont want to deal with that. I got the Collections.shuffle that works a lot easier than that. When i know there is an easier way then i come here. – Isaiah Mitchell May 10 '17 at 13:32
  • No you won't .... `new Random(),nextInt(array.length)` would give you have value between 0 and `array.length` (exclude). This is some simple code that you should be able to do yourself. Yes `shuffle` is simpler but you could do it without a List way simpler, just like the other answer – AxelH May 10 '17 at 13:36

2 Answers2

1

This program is a little rough... but it gets the job done. I have put the line-by-line explanation using comments. This program prints rocks and bushes in random places on the bottom row, and also prints a tall tree, which takes up 4 rows. Here's the program:

String[] choices = {"() ",".  "}; //choices holds 2 members, () and .
Random rand = new Random();       //creates random method
int place = rand.nextInt(10);     // this is for the tree
for (int j = 0; j<4; j++){
    for (int i = 0; i < 10; i++) {                //executes the following loop 10 times
        if (j==3){                                //makes rocks and bushes print only on bottom line
            int NumberOfAnswers = choices.length;     //holds value of number of members in choices
            int pick = rand.nextInt(NumberOfAnswers); // picks random int 0 or 1
            String finalChoice = choices[pick];       //"finalChoice" is either member 0 or 1 of choices
            System.out.print(finalChoice);            // prints "finalChoice"
        } else {
            System.out.print("   ");
        }
        if (i == place && j == 0) {                 //if it's the designated place and the top row...
            System.out.print(" ^  ");               //...print the top of the tree
        } else if (i==place && (j == 1||j == 2)) {  //if it's the designated place in the 2nd or 3rd rows...
            System.out.print("/|\\ ");              //...print the body of the tree
        } else if (i == place && j == 3){           //if its the designated place and the last row
            System.out.print(" |  ");               //...print the base of the tree
        }
    }   
    System.out.println();                         //start a new line
}

And 2 examples of the output:

-------------1-------------

    ^                             
   /|\                            
   /|\                            
.   |  () .  () .  .  () .  .  .  

-------------2-------------

                         ^        
                        /|\       
                        /|\       
() .  () () .  .  .  ()  |  () () 
  • Okay so I implemented the Collections.shuffle(); method and I got the output I desired. Now if I wanted to add a tree how would I do that. Since trees are taller and require more lines. Thats why in the first code i have 3 lines because the trees are like this ^ /|\ /|\ – Isaiah Mitchell May 09 '17 at 20:00
  • well basically what i was doing before was just printing /|\ but with \\ because I know its an escape charcter. But how would i be able to print it on more than one line to make it look like a tree and still have the bushes rocks and trees randomly generate. – Isaiah Mitchell May 10 '17 at 02:13
  • not exactly like that. I revised the question to show the final output that I desire. However with what you gave me I'm sure I can figure it out. Instead of a birds eye view which is kind of what you have im going for more of a side view or first person in a way. – Isaiah Mitchell May 10 '17 at 03:27
1

A simple approach would be to just add everthing to a single ArrayList and then use Collections.shuffle() on that list.

For example:

public static String forestGen(int numberBushes, int numberRocks) {
    String bush = "( )";
    String rock = ".";
    ArrayList<String> elements = new ArrayList<>(numberBushes + numberRocks);
    for (int i = 0; i < numberBushes; i++) {
        elements.add(bush);
    }
    for (int i = 0; i < numberRocks; i++) {
        elements.add(rock);
    }

    Collections.shuffle(elements);

    StringBuilder sb = new StringBuilder();
    for(String element : elements) {
        sb.append(element).append(" ");
    }
    return sb.toString();
}

Creates outputs like

. ( ) . ( ) . ( ) ( ) . ( )
. ( ) . ( ) ( ) ( ) ( ) . . 
( ) ( ) . ( ) ( ) ( ) . . . 
QBrute
  • 4,405
  • 6
  • 34
  • 40
  • Thanks a lot but how would i go about adding something with multiple lines. Since a tree would be 3 lines im not sure how to randomly put it in between the bushes and rocks. – Isaiah Mitchell May 09 '17 at 20:03