Java 8 – Generate Random Number in Range

Learn to generate random numbers (integer, float, long or double) in a specified range (origin and bound) using new methods added in Java 8 in Random, SecureRandom and ThreadLocalRandom classes.

private final static Random RANDOM = new Random();

Integer r1  = RANDOM.nextInt(0, 100);       //A random number between 0 and 99
Float r2    = RANDOM.nextFloat(0.0f, 1.0f); //A random number between 0 and 1

1. New Methods Added in Java 8

Since Java 8, the Random, SecureRandom and ThreadLocalRandom classes provide the following methods to generate random numbers between the specified origin (min) and bound (max).

In each method, the origin is inclusive, and the bound is exclusive.

  • nextInt(origin, bound): returns a pseudorandomly chosen integer value between the specified origin and bound.
  • nextFloat(origin, bound): returns a pseudorandomly chosen float value between the specified origin and bound.
  • nextLong(origin, bound): Returns a pseudorandomly chosen long value between the specified origin and bound.
  • nextDouble(origin, bound): Returns a pseudorandomly chosen double value between the specified origin and bound.

2. Difference between Random, SecureRandom and ThreadLocalRandom

The Random is the parent class of SecureRandom and ThreadLocalRandom and is used to generate a stream of pseudorandom numbers. Internally it relies on the system’s clock to generate the seeds, which isn’t truly random.

As discussed, the origin is inclusive, and the bound is exclusive, we can add 1 to the bound when generating the random integer and long values so the bound can also be considered a random number.

private final static Random RANDOM = new Random();

public static Integer getNextRandomInteger(int min, int max) {
  return RANDOM.nextInt(min, max + 1);
}

public static Float getNextRandomFloat(float min, float max) {
  return RANDOM.nextFloat(min, max);
}

Also, instances of java.util.Random are not cryptographically secure. It is recommended to use java.security.SecureRandom to get a cryptographically secure pseudo-random number generator for use by security-sensitive applications.

private final static SecureRandom SECURE_RANDOM = new SecureRandom();

public static Integer getNextSecureRandomInteger(int min, int max) {
  return SECURE_RANDOM.nextInt(min, max + 1);
}

public static Float getNextSecureRandomFloat(float min, float max) {
  return SECURE_RANDOM.nextFloat(min, max);
}

Although, instances of java.util.Random are threadsafe, its concurrent use across threads may encounter contention and consequent poor performance. The ThreadLocalRandom is recommended in concurrent applications.

public static Integer getNextThreadLocalRandomInteger(int min, int max) {
  return ThreadLocalRandom.current().nextInt(min, max + 1);
}

public static Float getNextThreadLocalRandomFloat(float min, float max) {
  return ThreadLocalRandom.current().nextFloat(min, max);
}

We can create more methods to generate random numbers of type long and double, in a similar fashion.

3. Generate Random Numbers within Range

Let us take a few examples of using the above functions in a Java program. We are using the Random class. You can replace Random with SecureRandom or ThreadLocalRandom according to your requirements.

3.1. Generate Random Integers between 0 and 100

The following Java program generates a few random integer values between 0 (origin) and 100 (bound).

Integer randomInt1 = getNextRandomInteger(0, 100);	//22
Integer randomInt2 = getNextRandomInteger(0, 100);	//34
Integer randomInt3 = getNextRandomInteger(0, 100);	//87

3.2. Generate Random Floats between 0 and 1

The following Java program generates a few random floating-point values between 0.0f (origin) and 1.0f (bound).

Float randomFloat1 = getNextRandomFloat(0.0f, 1.0f)
Float randomFloat2 = getNextRandomFloat(0.0f, 1.0f)
Float randomFloat3 = getNextRandomFloat(0.0f, 1.0f)

3.3. Generate Random Long Values between 1000 and 9999

The following Java program generates a few random long values between 1000 (origin) and 9999 (bound).


private final static Random RANDOM = new Random();

public static Long getNextRandomLong(long min, long max) {
  return RANDOM.nextLong(min, max + 1);
}

Long value1 = getNextRandomLong(1000L, 9999L);
Long value2 = getNextRandomLong(1000L, 9999L);
Long value3 = getNextRandomLong(1000L, 9999L);

4. Conclusion

In this short Java tutorial, we learned to generate random numbers in a range using the Random class and its subclasses SecureRandom and ThreadLocalRandom. We also learned the difference between these classes to understand better when to use them.

Happy Learning !!

Sourcecode on Github

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.