0

I get the general gist of for loops. I want to know how I could add two variables to the initializer. I also want to count counter and random at the same time. I want it to prints random but not to print 30 of the same number

public class forLoop {
  public static void main(String[] args) {
    int random = (int) (Math.random() *50) +25;
    for(int counter = 0; counter < 30; counter++){
      System.out.println(random);
    }
  }
}
Dariusz
  • 21,561
  • 9
  • 74
  • 114
Bill
  • 29
  • 4
  • 1
    Instead of asking this question you should ask yourself: "what does my program do?" It generates a pseudo-random number and prints it 30 times. How to make it print a random number 30 times? Generate the number and print it, repeat the whole thing 30 times. – Dariusz Jul 23 '13 at 07:00
  • You should also take a look at this: http://stackoverflow.com/questions/738629/math-random-versus-random-nextintint – Adrian Jandl Jul 23 '13 at 07:00

2 Answers2

4

You're generating the random number outside of your loop. Therefore it will exist as the same number every time. The solution is to move the definition inside of the loop.

public static void main(String[] args) {
    for(int counter = 0; counter < 30; counter++){
        int random = (int) (Math.random() *50) +25;
        System.out.println(random);
    }
}

In this way, every time through the loop (30 iterations), your code will (1) generate some random number and (2) print that number.

Kon
  • 10,702
  • 6
  • 41
  • 58
  • So I wouldn't need to add the random variable? – Bill Jul 23 '13 at 06:55
  • What do you mean add it, @Bill ? – Kon Jul 23 '13 at 06:57
  • It doesn't have to do with this one in particular now, but just for the future could I use two variables in a for loop? – Bill Jul 23 '13 at 07:01
  • I meant not add it to the for loop – Bill Jul 23 '13 at 07:02
  • "could I use two variables in a for loop?" Absolutely, yes. The body of a loop is a code block like any other. Everything you can do in Java, you can do inside of a loop (including starting another loop, defining variables, etc.) Be aware that variables you define INSIDE of your loop go out of scope, meaning they disappear, after your loop is finished with all of its iterations. – Kon Jul 23 '13 at 07:03
  • I mean inside the parenthesis of the for, could I define multiple variables there? – Bill Jul 23 '13 at 07:09
  • In general you should try these things to learn it the best, but yes you can define multiple variables in the declaration portion of the for-loop. `for (int i = 0, j = 100; i < j; i++, j--) { }` works – Kon Jul 23 '13 at 07:29
1

random is getting set to a particular random integer before the loop starts. You are not defining random to be (int) (Math.random() *50) +25 but rather you are executing that and setting random to the result.

The loop then prints out the same thing each time. If you want a new random each time, then you will need to move that statement inside the loop.

WW.
  • 23,793
  • 13
  • 94
  • 121