Jump to Real's How-to Main page

Get a random number

JDK1.1, Random.nextInt() returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence.
// random number between 0 AND 10
import java.util.Random;

Random r = new Random();
int randint = Math.abs(r.nextInt()) % 11;
JDK1.2, Random.nextInt(n) returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and n (exclusive).
// random number between 0 AND 10
import java.util.Random;

Random r = new Random();
int randint = r.nextInt(10);

If you find this article useful, consider making a small donation
to show your support for this Web site and its content.

Written and compiled by Réal Gagnon ©1998-2005
[ home ]