Java: random long number in 0 <= x < n range
Generate a random long number, in the [0, n)
range, using Java's ThreadLocalRandom.current().nextLong(n)
:
Voila! This generates a random long that strictly stays within 0 (included) and n (excluded).
Selecting the right toolkit
For multi-threaded environments: ThreadLocalRandom
In a thread-happy application, ThreadLocalRandom
is a blessing in disguise due to superior performance in such environments. It also lets you set origin and bound with precision:
The above generates a random long value that shakes hands with origin (included) and bids adieu to bound (excluded).
For reproduceable sequences: SplittableRandom
When tests call for identical sequences of randomness, guess who splits the mic? SplittableRandom
:
By tossing in a seed, you can replant the same random sequence anytime, a nifty trait when you need consistent test results.
Diving into alternatives and steering clear of pitfalls
The old school way: Math.random()
When no fancy libraries or utility classes accompany you, Math.random() stands by your side, combined with a touch of explicit casting:
A red flag: modulo bias
It's tempting to play with the modulo operator to cage a random long, but beware! It may open the door to bias:
This leads to an uneven distribution of numbers across the range, creating a bias. Refrain from this to ensure fair randomness across the spectrum.
Handling the extreme cases
When building a custom bounded method or using any random generation technique, pause and ponder — are all edge cases handled? Consider when n
is zero, negative, or busts the normal numeric limits.
Fancy features with Apache Commons Lang
Craving more candy on your plate? Meet Apache Commons Lang's RandomUtils
:
A cup of Apache commons offers lower and upper bound flavored randomness through RandomUtils.nextLong()
method.
Tackling the trade-offs
Count the cost for your specific use-case. Although ThreadLocalRandom
charms concurrent scenarios, SplittableRandom
might amp up the performance for single-thread contexts. Weigh the capabilities against the requirements to pick the most apt approach for your random generation needs.
Was this article helpful?