Random Number Generator in Java

generate random numbers in java on macbookGenerating random numbers in Java is a common task.

It’s frequently used in gambling, cryptography, statistical sampling and other areas where you need to simulate unpredictable behavior.

I’ll explain to you how to create random number generator and show few a little bit different ways how to do that.

How To Generate Random Range in Java

Usually, we want to generate a random integer in range.

That means we should create a function, that will generate a random number between min and max value.

Java Core provides 4 classes to do that:

Use can use libraries as well:

What to use?

In the most cases java.util.Random is your the best choice.

It’s thread-safe and can generate random integers for you – usually, it’s more than enough.

Random class has 2 little problems:

  • it’s not cryptographically secure.
  • it’s thread-safe, but the concurrent use of the same java.util.Random instance across threads may encounter contention and consequent poor performance

So if you have multithreading environment with a big amount of threads, that should use a random generator instance at the same time – java.util.concurrent.ThreadLocalRandom is your way to go.

If you need a cryptographically secure random generator – use java.security.SecureRandom .

Math.random()  generates a random double number and uses Random class internally to do that.

Let’s take a look at code examples.


java.util.Random class has a lot of methods, but nextInt() is the most popular. That’s why I’ll show you an example with it.

That’s why I’ll show you an example of it:

Our random(min, max) method will generate a pseudorandom number in a range between [min, max).

Min value will be inclusive and max will be exclusive.


Random Number Generator in Java using java.util.RandomClick To Tweet


This method generates a random double number. Internally on the first call, it creates an instance of java.util.Random class and uses it to generate a value.

It’s the same if you call java.util.Random.nextDouble() .


The method will generate a pseudorandom integer from min (including) to max (excluding).


Random Number Generator in Java using java.lang.MathClick To Tweet

ThreadLocalRandom.nextInt(min, max)

A random number generator isolated to the current thread, so you can use it in the concurrent environment without any problems.

Since Java 8 it’s protected with @Contended annotation against false sharing.


One more benefit – ThreadLocaleRandom class provides commonly used method to generate a number in a range, just specify min and max values and that’s it.


Random Number Generator in Java using ThreadLocaleRandomClick To Tweet


Provides cryptographically strong random number generator.

It passes statistical random generator tests.

Usually, SecureRandom is used in the following way:

By default SecureRandom class uses PRNG algorithm.

It could be switched like this:


Random Number Generator in Java using SecureRandomClick To Tweet

RandomUtils.nextInt(min, max)

It’s just a wrapper on example, that I provided for a java.util.Random class.

To use RandomUtils you should add maven dependency:

Method signature looks like this:

RandomUtils provides methods to generate double, float, long and bytes as well.


Random Number Generator in Java using RandomUtilsClick To Tweet

RandomDataGenerator.nextInt(min, max)

RandomDataGenerator uses WELL19937c pseudo-random number generator to generate the data.

To use it you need to include the following dependency to your pom.xml

A method that generates an integer value looks like that:

It works in the same way as previous code examples.

Library provides a big amount of methods to generate a different kind of data.

It could be useful for advanced use cases.


Random Number Generator in Java using RandomDataGeneratorClick To Tweet
1 Star2 Stars3 Stars4 Stars5 Stars
Scroll Up