Java Algorithms How to - Generate two seperate outputs using Random








Question

We would like to know how to generate two seperate outputs using Random.

Answer

import java.util.Random;
/*from   w  w  w.j  a v a 2s  .  c o m*/
public class Main {
  private static final Random rand = new Random();

  public static void main(String[] args) {
    int i = 1;
    while (i <= 100) {
      System.out.printf("%-5d", rand.nextInt(4) + 4);
      if (i % 10 == 0) {
        System.out.println();
      }
      i++;
    }
    System.out.println();
    i = 1;// modified here
    while (i <= 100) {
      System.out.printf("%-5d", rand.nextInt(10 * (80) + 10));
      if (i % 10 == 0) {
        System.out.println();
      }
      i++;
    }
  }
}

The code above generates the following result.