Java Random
Java Random class instance is used to generate a stream of pseudorandom numbers.
In this tutorial, we will learn about he constructors and methods of Java Random class with example programs.
Java Random Class Constructors
There are two constructors to instantiate a Random class object.
1. Random()
Random() creates a new random number generator.
In the following example, we create an instance of Random class using Random() constructor.
Java Program
import java.util.Random;
public class Example{
public static void main(String[] args) {
Random random = new Random();
}
}
2. Random(long seed)
Random() creates a new random number generator using the specified seed.
In the following example, we create an instance of Random class using Random(seed) constructor.
Java Program
import java.util.Random;
public class Example {
public static void main(String[] args) {
long seed = 412;
Random random = new Random(seed);
}
}
Java Random Class Methods
1. doubles()
Random.doubles() returns an effectively unlimited stream of pseudorandom double values, each between zero (inclusive) and one (exclusive).
In the following example, we will generate an unlimited sequence of random double-precision floating point numbers using doubles() method and print out four of them to the console.
Java Program
import java.util.Random;
import java.util.function.DoubleConsumer;
import java.util.stream.DoubleStream;
public class Example {
public static void main(String[] args) {
Random random = new Random();
DoubleStream ds = random.doubles();
ds.limit(4).forEach(new DoubleConsumer() {
@Override
public void accept(double value) {
System.out.println(value);
}
});
}
}
Output
0.14584589858798724
0.01596337682890825
0.4461234706535546
0.5966428933676975
2. doubles(double randomNumberOrigin, double randomNumberBound)
Random.doubles() Returns an effectively unlimited stream of pseudorandom double values, each conforming to the given origin (inclusive) and bound (exclusive).
In the following example, we will generate an unlimited sequence of random double-precision floating point numbers, which are limited by an origin and bound using doubles() method and print out four of these numbers from the stream to the console.
Java Program
import java.util.Random;
import java.util.function.DoubleConsumer;
import java.util.stream.DoubleStream;
public class Example {
public static void main(String[] args) {
double randomNumberOrigin = 1;
double randomNumberBound = 7;
Random random= new Random();
DoubleStream ds = random.doubles(randomNumberOrigin, randomNumberBound);
ds.limit(4).forEach(new DoubleConsumer() {
@Override
public void accept(double value) {
System.out.println(value);
}
});
}
}
Output
4.124126976639339
6.277075813743702
2.9896554435576643
1.6867903457923643
3. doubles(long streamSize)
Random.doubles() returns a stream producing the given streamSize number of pseudorandom double values, each between zero (inclusive) and one (exclusive).
In the following example, we will generate an specific number of random double-precision floating point numbers, using doubles(streamSize) method and print out these random numbers from the stream to the console.
Java Program
import java.util.Random;
import java.util.function.DoubleConsumer;
import java.util.stream.DoubleStream;
public class Example {
public static void main(String[] args) {
long streamSize = 5;
Random random = new Random();
DoubleStream ds = random.doubles(streamSize);
ds.forEach(new DoubleConsumer() {
@Override
public void accept(double value) {
System.out.println(value);
}
});
}
}
Output
0.24585762538765643
0.8752279248291293
0.6706517385958398
0.47129774709285743
0.8528845262464368
4. doubles(long streamSize, double randomNumberOrigin, double randomNumberBound)
Random.doubles() returns a stream producing the given streamSize number of pseudorandom double values, each conforming to the given origin (inclusive) and bound (exclusive).
In the following example, we will generate eight random double-precision floating point numbers which are limited by an origin and bound using doubles() method and print out these random numbers from the stream to the console.
Java Program
import java.util.Random;
import java.util.function.DoubleConsumer;
import java.util.stream.DoubleStream;
public class Example {
public static void main(String[] args) {
double randomNumberOrigin = 1;
double randomNumberBound = 7;
long streamSize = 8;
Random random = new Random();
DoubleStream ds =random.doubles(streamSize,randomNumberOrigin,randomNumberBound);
ds.forEach(new DoubleConsumer() {
@Override
public void accept(double value) {
System.out.println(value);
}
});
}
}
Output
2.392701685665302
2.9381978153219777
3.69356795462521
6.0812621302928305
5.793783278818572
5.451979737142773
1.8253898408924665
2.54037873488756
5. ints()
Random.ints() returns an effectively unlimited stream of pseudorandom int values.
In the following example, we will generate an unlimited sequence of random integers using ints() method and print out four of them to the console.
Java Program
import java.util.Random;
import java.util.stream.IntStream;
import java.util.function.IntConsumer;
public class Example {
public static void main(String[] args) {
Random random = new Random();
IntStream ds = random.ints();
ds.limit(4).forEach(new IntConsumer() {
@Override
public void accept(int value) {
System.out.println(value);
}
});
}
}
Output
1549243955
-654704097
-1217687007
1648661790
6. ints(int randomNumberOrigin, int randomNumberBound)
Random.ints() returns an effectively unlimited stream of pseudorandom int values, each conforming to the given origin (inclusive) and bound (exclusive).
Java Program
import java.util.Random;
import java.util.function.IntConsumer;
import java.util.stream.IntStream;
public class Example {
public static void main(String[] args) {
int randomNumberOrigin = 1;
int randomNumberBound = 7;
Random random= new Random();
IntStream ds = random.ints(randomNumberOrigin, randomNumberBound);
ds.limit(5).forEach(new IntConsumer() {
@Override
public void accept(int value) {
System.out.println(value);
}
});
}
}
Output
2
2
1
1
5
7. ints(long streamSize)
Random.ints() returns a stream producing the given streamSize number of pseudorandom int values.
In the following example, we will generate eight random integers using ints(streamSize) method and print out these random numbers from the stream to the console.
Java Program
import java.util.Random;
import java.util.function.IntConsumer;
import java.util.stream.IntStream;
public class Example {
public static void main(String[] args) {
int streamSize = 8;
Random random = new Random();
IntStream ds = random.ints(streamSize);
ds.forEach(new IntConsumer() {
@Override
public void accept(int value) {
System.out.println(value);
}
});
}
}
Output
790793602
-445498747
-322809516
1575745272
732465345
586364815
1791511337
-1366525847
8. ints(long streamSize, int randomNumberOrigin, int randomNumberBound)
Random.ints() Returns a stream producing the given streamSize number of pseudorandom int values, each conforming to the given origin (inclusive) and bound (exclusive).
In the following example, we will generate eight random integers which are limited by an origin 1
and bound 7
using ints() method and print out these random numbers from the stream to the console.
Java Program
import java.util.Random;
import java.util.function.IntConsumer;
import java.util.stream.IntStream;
public class Example {
public static void main(String[] args) {
int streamSize = 8;
int randomNumberOrigin = 1;
int randomNumberBound = 7;
Random random = new Random();
IntStream ds = random.ints(streamSize, randomNumberOrigin, randomNumberBound);
ds.forEach(new IntConsumer() {
@Override
public void accept(int value) {
System.out.println(value);
}
});
}
}
Output
4
6
2
5
5
3
3
2
9. longs()
Random.longs() returns an effectively unlimited stream of pseudorandom long values.
In the following example, we will generate an unlimited sequence of random long type numbers using longs() method and print out four of them to the console.
Java Program
import java.util.Random;
import java.util.function.LongConsumer;
import java.util.stream.LongStream;
public class Example {
public static void main(String[] args) {
Random random = new Random();
LongStream ds = random.longs();
ds.limit(4).forEach(new LongConsumer() {
@Override
public void accept(long value) {
System.out.println(value);
}
});
}
}
Output
-6251239855392854015
-7974780473092051021
-2652163404078734145
7738474359912325515
10. longs(long streamSize)
Random.longs() returns a stream producing the given streamSize number of pseudorandom long values.
In the following example, we will generate an specific number of long values, say eight, using longs(streamSize) method and print out these random numbers from the stream to the console.
Java Program
import java.util.Random;
import java.util.function.LongConsumer;
import java.util.stream.LongStream;
public class Example {
public static void main(String[] args) {
long streamSize = 8;
Random random = new Random();
LongStream ds = random.longs(streamSize);
ds.forEach(new LongConsumer() {
@Override
public void accept(long value) {
System.out.println(value);
}
});
}
}
Output
-6830441378515779608
-1179248027058137291
-5606058920192108368
8178120771225726595
-3472126381625498085
7782182188885249485
-4504231265999262239
-4107022727140179889
11. longs(long randomNumberOrigin, long randomNumberBound)
Random.longs() returns an effectively unlimited stream of pseudorandom long values, each conforming to the given origin (inclusive) and bound (exclusive).
In the following example, we will generate an unlimited sequence of random long values, which are limited by an origin and bound using longs() method and print out four of these numbers from the stream to the console.
Java Program
import java.util.Random;
import java.util.function.LongConsumer;
import java.util.stream.LongStream;
public class Example {
public static void main(String[] args) {
long randomNumberOrigin = 154;
long randomNumberBound = 200;
Random random = new Random();
LongStream ds = random.longs(randomNumberOrigin, randomNumberBound);
ds.limit(4).forEach(new LongConsumer() {
@Override
public void accept(long value) {
System.out.println(value);
}
});
}
}
Output
188
174
194
188
12. longs(long streamSize, long randomNumberOrigin, long randomNumberBound)
Random.longs() returns a stream producing the given streamSize number of pseudorandom long, each conforming to the given origin (inclusive) and bound (exclusive).
In the following example, we will generate eight random numbers of type long which are limited by an origin and bound using longs() method and print out these random numbers from the stream to the console.
Java Program
import java.util.Random;
import java.util.function.LongConsumer;
import java.util.stream.LongStream;
public class Example {
public static void main(String[] args) {
long streamSize = 8;
long randomNumberOrigin = 154;
long randomNumberBound = 200;
Random random = new Random();
LongStream ds = random.longs(streamSize, randomNumberOrigin, randomNumberBound);
ds.forEach(new LongConsumer() {
@Override
public void accept(long value) {
System.out.println(value);
}
});
}
}
Output
192
172
155
170
179
157
188
197
13. next(int bits)
Random.next() returns the next pseudorandom number from this Random Generator’s sequence.
Random.next() is a protected method of Random class. So, to use this class, we may have to create a class, say Example
with Random
as its parent class. Since, Example
is a child class of Random
, Example
can access protected class of Random.
Java Program
import java.util.Random;
public class Example extends Random{
public static void main(String[] args) {
Example random = new Example();
System.out.println(" Next Random Value : " + random.next(9));
System.out.println(" Next Random Value : " + random.next(15));
System.out.println(" Next Random Value : " + random.next(32));
System.out.println(" Next Random Value : " + random.next(4));
}
}
Output
Next Random Value : 174
Next Random Value : 10188
Next Random Value : -776918100
Next Random Value : 9
14. nextBoolean()
Random.nextBoolean() returns the next pseudorandom, uniformly distributed boolean value from this random number generator’s sequence.
In the following example, we will create an object random
of Random
class type. We will call nextBoolean() on this Random object to get the next random boolean value. We shall print it to console.
Java Program
import java.util.Random;
public class Example{
public static void main(String[] args) {
Random random = new Random();
boolean b = random.nextBoolean();
System.out.println("Next random boolean value is : " + b);
}
}
Output
Next random boolean value is : true
15. nextBytes(byte[] bytes)
Random.nextBytes() generates random bytes and places them into a user-supplied byte array.
In the following example, we will create an object random
of Random
class type and a an empty byte array of size 10. We will call nextBytes() on this Random object and pass the empty byte array to the method as argument. The method loads this byte array with the randomly generated bytes.
Java Program
import java.util.Random;
public class Example{
public static void main(String[] args) {
Random random = new Random();
byte[] bytes = new byte[10];
random.nextBytes(bytes);
System.out.println("Random bytes : ");
for(byte b: bytes) {
System.out.println(b);
}
}
}
Output
Random bytes :
-61
91
69
107
-128
-69
-42
107
53
-102
16. nextDouble()
Random.nextDouble() returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator’s sequence.
In the following example, we will create an object random
of Random
class type. We will call nextDouble() on this Random object to get the next random double value. We shall print it to console.
Java Program
import java.util.Random;
public class Example{
public static void main(String[] args) {
Random random = new Random();
double d = random.nextDouble();
System.out.println("Next random double value is : " + d);
}
}
Output
Next random double value is : 0.2885113742924815
17. nextFloat()
Random.nextFloat() Returns the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from this random number generator’s sequence.
In the following example, we will create an object random
of Random
class type. We will call nextFloat() on this Random object to get the next random float value. We shall print it to console.
Java Program
import java.util.Random;
public class Example{
public static void main(String[] args) {
Random random = new Random();
float f = random.nextFloat();
System.out.println("Next random float value is : " + f);
}
}
Output
Next random float value is : 0.9188673
18. nextGaussian()
Random.nextGaussian() returns the next pseudorandom, Gaussian (“normally”) distributed double value with mean 0.0 and standard deviation 1.0 from this random number generator’s sequence.
In the following example, we will create an object random
of Random
class type. We will call nextGaussian() on this Random object to get the next Gaussian double value. We shall print it to console.
Java Program
import java.util.Random;
public class Example{
public static void main(String[] args) {
Random random = new Random();
double g = random.nextGaussian();
System.out.println("Next random gaussian value is : " + g);
}
}
Output
Next random gaussian value is : 0.05262116402960923
19. nextInt()
Random.nextInt() returns the next pseudorandom, uniformly distributed int value from this random number generator’s sequence.
In the following example, we will create an object random
of Random
class type. We will call nextInt() on this Random object to get the next integer value. We shall print it to console.
Java Program
import java.util.Random;
public class Example{
public static void main(String[] args) {
Random random = new Random();
int i = random.nextInt();
System.out.println("Next random integer value is : " + i);
}
}
Output
Next random integer value is : 759359738
20. nextInt(int bound)
Random.nextInt() Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator’s sequence.
In the following example, we will create an object random
of Random
class type. We will call nextInt(bound) on this Random object to get the next integer value within the number, bound
. We shall print it to console.
Java Program
import java.util.Random;
public class Example{
public static void main(String[] args) {
Random random = new Random();
int bound = 100;
int i = random.nextInt(bound);
System.out.println("Next random integer value is : " + i);
}
}
Output
Next random integer value is : 53
21. nextLong()
Random.nextLong() returns the next pseudorandom, uniformly distributed long value from this random number generator’s sequence.
In the following example, we will create an object random
of Random
class type. We will call nextLong() on this Random object to get the next long value. We shall print this long value to console.
Java Program
import java.util.Random;
public class Example{
public static void main(String[] args) {
Random random = new Random();
long l = random.nextLong();
System.out.println("Next random long value is : " + l);
}
}
Output
Next random long value is : 5015315584966125576
22. setSeed(long seed)
Random.setSeed() method sets the seed of this random number generator using a single long seed.
In the following example, we will create a Random class object and set the seed for this Random object.
Java Program
import java.util.Random;
public class Example {
public static void main(String[] args) {
Random random = new Random();
long seed = 20;
random.setSeed(seed);
System.out.println("The seed is set for the random object.");
System.out.println("Next Random Int : "+random.nextInt());
}
}
Output
The seed is set for the random object.
Next Random Int : -1150867590
Conclusion
In this Java Tutorial, we have learnt the constructors and methods of Java Random class with the help of examples.