Languages
[Edit]
EN

Java - random int number in both inclusive range with threads

4 points
Created by:
Tehya-Blanchard
474

In this short article we would like to show how to generate both inclusive random numbers in a range inside threads using Java. We want to generate numbers using built-in Java API.

Motivation to use below solution is better performance than just Random class when we work with many threads. ThreadLocalRandom provides separated randomizers related with specific thread that generate numbers in independent way.

Quick solution:

Usage example:

Output:

Note: ThreadLocalRandom.current() returns ThreadLocalRandom object for current thread.

References

Edit
  1. Class ThreadLocalRandom - Oracle Docs 

1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join