Lewis D.
—You need to generate random integers within a specific range. Is there a way to do it in Java?
There are a number of ways to generate random integers confined to a specific range in Java, and the problem is simplified by the several approaches the built-in Java libraries offer.
Let’s take a look at a couple of solutions that rely on core Java, excluding the numerous third-party libraries offering similar functionality.
We’ll consider the following randomized range integer-generating approaches:
The best solution for your needs depends on the application you need random integers to be generated for.
We can use the java.util.Random
class to create a new random number generator, which we can then use to retrieve a pseudorandom, uniformly distributed int
value.
To get our random number, we’ll call the nextInt(int bound)
method. This method returns an int
in the range starting at 0 up to, but not including, the bound value we supply. Let’s take a closer look:
public int randomRangeRandom(int start, int end) { Random random = new Random(); int number = random.nextInt((end - start) + 1) + start; // see explanation below return number; }
Here, the bound value calculation defines the upper value in our range. For example, if we set our start
and end
to 5 and 10 respectively, we’ll pass in 6 ((end - start) + 1
) as our bound. The nextInt()
method call will generate a number between 0 and 6 (exclusive) and then we’ll add the start value (5) which gets us back into our desired range.
We can also use the java.security.SecureRandom
class when we require cryptographically strong random numbers. The SecureRandom
class is designed to return a non-deterministic output, which is useful for high unpredictability but comes at a performance cost. Because SecureRandom
extends Random
, the code example for randomRangeRandom()
can be followed, initializing the SecureRandom
class instead of the Random
class.
Note: Make sure you have imported java.security.SecureRandom
or java.security.*
because while Random
is in java.util
, SecureRandom
is in java.security
.
The third random number generating approach is best for multithreaded applications. Like SecureRandom
, the java.util.concurrent.ThreadLocalRandom
class also extends Random
, and is therefore a thread-safe substitute for Random
. Note that ThreadLocalRandom
does not inherit any attributes of SecureRandom
, so it’s not a cryptographically secure random number generator.
public int randomRangeThreadLocalRandom(int start, int end) { int number = ThreadLocalRandom.current().nextInt(start, end); return number; }
Note: Make sure you import java.util.concurrent.*
or java.util.concurrent.ThreadLocalRandom
.
Finally, we can generate a random number using the java.lang.Math
class. The Math
class provides a random()
method, but this method returns a double value from 0.0 (inclusive) to 1.0 (exclusive). This means we will have to write some additional code to generate an int
value within our specified range.
To make this clearer, let’s look at the output of executing each step when calling randomRangeMath(5, 10)
:
public int randomRangeMath(int start, int end) { int range = end - start; // calculate our range: 5 double randomDouble = Math.random(); // returns a double: 0.3 double calc = (randomDouble * range) + start; // calculation returns 6.5 long number = Math.round(calc); // 6.5 is rounded up to 7 return (int) number; }
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at [email protected].
If you are a California resident, see our Supplemental notice.