I know that Random
class generates insecure random sequences and I should prefer using SecureRandom
when dealing with security. But what about ThreadLocalRandom
? Is it more or less secure?
// generate a five-digit numeric confirmation code
Long code = ThreadLocalRandom.current().nextLong(1, 99999);
Use of ThreadLocalRandom is particularly appropriate when multiple tasks (for example, each a ForkJoinTask ) use random numbers in parallel in thread pools. Usages of this class should typically be of the form: ThreadLocalRandom. current().
A ThreadLocalRandom is initialized using an internally generated seed value that is not otherwise modifiable. Using ThreadLocalRandom instead of shared instances of Random will result in low contention and overhead. ThreadLocalRandom, just like its parent class, is not cryptographically secure.
As described in its javadoc, ThreadLocalRandom
is similar to Random
(i.e. not secure) but with better performance in case of concurrent access.
Instances of
ThreadLocalRandom
are not cryptographically secure. Consider instead usingSecureRandom
in security-sensitive applications.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With