In this Java tutorial, you will learn how to generate random numbers using the random() method of the Mathclass and methods of the java.util.Random class.

Remember that the generated numbers are actually pseudorandom numbers, or “fake” random numbers. The generated numbers are parts of a very large sequence so they appear to be random, but they are not true random numbers. However, you can use it for cases in which true randomness and security are not required.

 

1. Generate random numbers using Math.random()

The static method random() of the Math class returns a pseudorandom double value in the range from 0.0 to 1.0. The following code generates a random integer number between 1 and 10 (1 <= x <= 10):

int x = 1 + (int) (Math.random() * 10);

And the following code snippet generates 10 random integer numbers between 1 and 10:

for (int i = 1; i <= 10; i++) {
	int x = 1 + (int) (Math.random() * 10);
	System.out.println(x);
}

This code creates an array of 100 random numbers in the range from 1 to 10:

int[] numbers = new int[100];

for (int i = 0; i < numbers.length; i++) {
	numbers[i] = (int) (Math.random() * 10);
}

Similarly, the following code generates 10 random numbers between 1 and 100:

for (int i = 1; i <= 10; i++) {
	int x = 1 + (int) (Math.random() * 100);
	System.out.println(x);
}

And the following example generates random numbers in the range from 60 to 180:

for (int i = 1; i <= 10; i++) {
	int x = 60 + (int) (Math.random() * 180);
	System.out.println(x);
}

NOTE: The Math.random() method uses the java.util.Random class internally. It calls the nextDouble() method of the Random class that returns a pseudorandom double value between 0.0 and 1.0.


2. Generate random numbers using java.util.Random class

Randomis the base class that provides convenient methods for generating pseudorandom numbers in various formats like integer, double, long, float, boolean and you can even generate an array of random bytes. It is implemented under the java.util package so you need to import this class from this package in your code.

You can create a new instance of the Random class by using its empty constructor:

Random random = new Random();

or by providing a long seed - the initial value of the internal state of the pseudorandom number generator. For example:

long seed = System.currentTimeMillis();
Random random = new Random(seed);

A random number generator produces pseudorandom numbers in a determinable sequence or pattern, and the seed value specifies the starting point in the sequence, so two Random instances constructed with the same seed will produce the same sequence of pseudorandom numbers.

Therefore, you can use the empty constructor in most cases. Use the seed parameter constructor when debugging the code with the same sequence of pseudorandom numbers.

The nextBoolean() method returns the next random boolean value, for example:

boolean b = random.nextBoolean();

 

The nextBytes(byte[] bytes)method generates random bytes and put them into the specified byte array, for example:

byte[] bytes = new byte[64];
random.nextBytes(bytes);

 

The nextDouble() method returns the next random double value between 0.0 and 1.0. This method is used by the Math.random() method. For example, the code to generate 10 random numbers between 1 and 10 using Random class can be written as follows:

Random random = new Random();

for (int i = 1; i <= 10; i++) {
	int x = 1 + (int) (random.nextDouble() * 10);
}

 

The nextFloat() method returns the next random float value between 0.0 and 1.

The nextInt() method returns the next random integer value with 2^32 possible int values.

The nextInt(int bound) method returns the next random integer value in the range from 0 (inclusive) to the specified bound value (exclusive). So the code to generate 10 random numbers between 1 and 100 can be written using nextInt() method as follows:

Random random = new Random();

for (int i = 1; i <= 10; i++) {
	int x = 1 + random.nextInt(100);
	System.out.println(x);
}

And the nextLong() method returns the next random long value.


3. Using Java Stream API for random numbers

From Java 8, the Random class provides some methods that return streams of random numbers. For example:

IntStream ints = random.ints();

This returns a stream of random int values. The number of values is unlimited.

The ints(long streamSize) method returns a limited stream of random int values. The number of values is specified by the streamSize. For example, the following code prints 10 random integer numbers:

IntStream ints = random.ints(10);
ints.forEach(System.out::println);

 

The ints(long streamSize, int randomNumberOrigin, int randomNumberBound)method returns a limited stream of random int values between randomNumberOrigin (inclusive) and randomNumberBound (exclusive). For example, the code to print 10 random integer numbers between 1 and 10 can be written as compactly as:

IntStream ints = random.ints(10, 1, 11);
ints.forEach(System.out::println); 

The Random class also provides similar methods for producing a stream of random long values.

 

Now, you can see there are at least 3 different ways to generate random numbers between 1 and 100 in Java.

Using Math.random() method:

for (int i = 1; i <= 10; i++) {
	int x = 1 + (int) (Math.random() * 100);
	System.out.println(x);
}

 

Using nextInt() method of Random class:

Random random = new Random();

for (int i = 1; i <= 10; i++) {
	int x = 1 + random.nextInt(100);
	System.out.println(x);
}

 

And using ints() method of Random class:

IntStream ints = random.ints(10, 1, 11);
ints.forEach(System.out::println); 

References:

 

Other Java Coding Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.

Add comment