Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid creating and discarding java.util.SecureRandom repeatedly #2

Open
fvasco opened this issue Jan 29, 2025 · 0 comments
Open

Avoid creating and discarding java.util.SecureRandom repeatedly #2

fvasco opened this issue Jan 29, 2025 · 0 comments
Labels
enhancement New feature or request

Comments

@fvasco
Copy link

fvasco commented Jan 29, 2025

Creating a SecureRandom instance may involve a blocking operation. At the same time, SecureRandom is thread-safe, so it's recommended to create a static instance and reuse it multiple times.

Finally, please consider that (int) max can be a negative number (ie: (int) Long.MAX_VALUE).

See:

new SecureRandom().nextBytes(bytes);

SecureRandom random = new SecureRandom();

return random.nextInt((int) max);

@ovx ovx added the enhancement New feature or request label Feb 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants