Get Random integers that range from from 0 to n in Java

Description

The following code shows how to get Random integers that range from from 0 to n.

Example


/*ww w.  j ava2s . co m*/
import java.util.Random;

public class Main {
  public static void main(String[] argv) throws Exception {

    Random rand = new Random();

    int n = 10;
    int i = rand.nextInt(n + 1);
    System.out.println(i);
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Development »




Java Algorithms
Java Clipboard
Java Compiler
Java Desktop
Java Virtual Machine
Java Math
OS
Random
Java Robot
Java RuntimeMXBean
Java Timer
Java UUID
Java Internationalization