Java Data Type How to - Create random number which is less than 11








Question

We would like to know how to create random number which is less than 11.

Answer

import java.util.Arrays;
import java.util.Random;
//  w  ww.  j  a  va2  s  .  c  om
public class Main {
  public static void main(String[] args) {

    Random rand = new Random();

    int randnumb;
    int array[] = new int[5];
    for (int j = 0; j <= 4; j++) {
      randnumb = 1 + rand.nextInt(11);
      array[j] = randnumb;
    }
    System.out.println(Arrays.toString(array));

  }
}

The code above generates the following result.