You can do

Random rand = new SecureRandom()
// 0 to 100 inclusive.
int number = rand.nextInt(101);

or

// 0 inclusive to 100 exclusive.
int number = rand.nextInt(100);

Note: this is more efficient than say (int) (rand.nexDouble() * 100) as nextDouble() needs to create at least 53-bits of randomness whereas nextInt(100) creates less than 7 bits.

Answer from Peter Lawrey on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › security › SecureRandom.html
SecureRandom (Java Platform SE 8 )
2 weeks ago - doubles, doubles, doubles, doubles, ... nextInt, nextInt, nextLong · clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait ... Constructs a secure random number generator (RNG) implementing the default random number algorithm. This constructor traverses ...
🌐
Baeldung
baeldung.com › home › security › the java securerandom class
The Java SecureRandom Class | Baeldung
February 20, 2025 - Learn how to use the SecureRandom class in Java and how to produce safe random numbers.
🌐
Java Guides
javaguides.net › 2023 › 09 › java-securerandom-nextint.html
Java SecureRandom nextInt()
September 28, 2023 - import java.security.SecureRandom; public class SecureRandomExample { public static void main(String[] args) { // Create a SecureRandom object SecureRandom secureRandom = new SecureRandom(); // Generate random integers within a given bound int randomInt1 = secureRandom.nextInt(100); // bound is 100 int randomInt2 = secureRandom.nextInt(1000); // bound is 1000 // Print the generated random integers System.out.println("Random Integer within 0 to 100: " + randomInt1); System.out.println("Random Integer within 0 to 1000: " + randomInt2); } }
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › security › SecureRandom.html
SecureRandom (Java Platform SE 7 )
This self-seeding will not occur if setSeed was previously called. ... Generates an integer containing the user-specified number of pseudo-random bits (right justified, with leading zeros). This method overrides a java.util.Random method, and serves to provide a source of random bits to all ...
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Random.html
Random (Java Platform SE 8 )
2 weeks ago - Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence. The general contract of nextInt is that one int value in the specified range is pseudorandomly generated and returned.
🌐
Tabnine
tabnine.com › home page › code › java › java.security.securerandom
java.security.SecureRandom.nextInt java code examples | Tabnine
private char[] generatePassphrase() { char[] result=new char[128]; for (int i=0; i<result.length; i++) { result[i]=BASE36_SYMBOLS.charAt(rng.nextInt(BASE36_SYMBOLS.length())); } return result; } } ... protected static String createSecKey() { SecureRandom random = new SecureRandom(); byte[] data = new byte[16]; for (int i = 0; i < 4; ++i) { int val = random.nextInt(); data[i * 4] = (byte) val; data[i * 4 + 1] = (byte) ((val >> 8) & 0xFF); data[i * 4 + 2] = (byte) ((val >> 16) & 0xFF); data[i * 4 + 3] = (byte) ((val >> 24) & 0xFF); } return Base64.getEncoder().encodeToString(data); }
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-util-random-nextint-java
Java.util.Random.nextInt() in Java - GeeksforGeeks
March 21, 2025 - java.security.SecureRandom: Although this method gives the random integer values for better security we can use the alternative SecureRandom which returns the cryptographically secure random numbers.
🌐
Oracle
docs.oracle.com › javase › 6 › docs › api › java › security › SecureRandom.html
SecureRandom (Java Platform SE 6)
This self-seeding will not occur if setSeed was previously called. ... Generates an integer containing the user-specified number of pseudo-random bits (right justified, with leading zeros). This method overrides a java.util.Random method, and serves to provide a source of random bits to all ...
Find elsewhere
Top answer
1 of 5
40

Yes it is secure.

Code examination of java.util.Random shows that ints() creates a spliterator that uses internalNextInt(...) to generate the random integers. That in turn calls nextInt() on this. In the case of java.security.SecureRandom, nextInt() is overridden to generate a "secure" random number1.

You can confirm this for yourself by looking at the source code.


1 - Of course, it doesn't actually make sense to call an integer or a sequence of integers "secure". And there are situations where SecureRandom may not have the properties that you require. (It depends on the actual RNG or PRNG implementation used by the class, the quality of the supplied seed or system provided entropy source, and so on.) But SecureRandom::ints() will generate a sequence of integers that has the same properties as if you made a sequence of SecureRandom::nextInt() calls on the same object. If the latter sequence is suitable for your purposes (whatever they are) then so is the former.

2 of 5
15

Random.ints() is a method that returns an IntStream. An IntStream is neither secure nor insecure: it's a stream of numbers.

The "security" of the sequence of ints returned by the method depends on the implementation of the method. SecureRandom generates its "random" values more securely than Random. They share the same API, and thus you can use either in a given context depending upon your requirements.

So, the fact it inherits from an insecure class is irrelevant to the security: you can reasonably trust that the SecureRandom class is as secure as the documentation says it is.


Consider an analogy with HashSet: this makes no guarantees of the iterator ordering; however, LinkedHashSet, a subclass of HashSet does guarantee iterator ordering. The guarantee of LinkedHashSet is consistent with the guarantee of HashSet, because a specific ordering is one of the possible orderings that could be observed with "no guaranteed ordering" (after all, you have to return the elements in some order).

Similarly, Random makes no guarantees about the security of the sequence of ints returned; SecureRandom makes stronger guarantees. But there is no reason why the sequence of ints from a SecureRandom couldn't also be returned by a Random, by coincidence.

🌐
Sentry
sentry.io › sentry answers › java › generating random numbers in java
Generating random numbers in Java | Sentry
September 15, 2024 - import java.security.SecureRandom; public class SecureDemo{ public static void main(String[] args) { SecureRandom secureRandom = new SecureRandom(); int n = secureRandom.nextInt(2); System.out.println(n); } } This code has the same functionality as the first example.
🌐
GitHub
github.com › openjdk › jdk › blob › master › src › java.base › share › classes › java › security › SecureRandom.java
jdk/src/java.base/share/classes/java/security/SecureRandom.java at master · openjdk/jdk
secureRandomSpi.engineNextBytes(bytes, params); } } } · /** * Generates an integer containing the user-specified number of · * pseudo-random bits (right justified, with leading zeros). This · * method overrides a {@code java.util.Random} method, and serves · * to provide a source of random bits to all the methods inherited · * from that class (for example, {@code nextInt}, * {@code nextLong}, and {@code nextFloat}).
Author   openjdk
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.security.securerandom.next
SecureRandom.Next(Int32) Method (Java.Security) | Microsoft Learn
Generates an integer containing the user-specified number of pseudo-random bits (right justified, with leading zeros). This method overrides a java.util.Random method, and serves to provide a source of random bits to all of the methods inherited ...
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › security › SecureRandom.html
SecureRandom (Java SE 11 & JDK 11 )
January 20, 2026 - Otherwise, this class will instead synchronize access to the following methods of the SecureRandomSpi implementation: ... doubles, doubles, doubles, doubles, ints, ints, ints, ints, longs, longs, longs, longs, nextBoolean, nextDouble, nextFloat, nextGaussian, nextInt, nextInt, nextLong
🌐
TutorialsPoint
tutorialspoint.com › random-vs-secure-random-numbers-in-java
Random vs Secure Random numbers in Java
int randonInt2 = objRandom.nextInt(1000); System.out.println("Random Integers: " + randomInt1); System.out.println("Random Integers: " + randonInt2); } } ... import java.security.SecureRandom; public class SecureRandomClass { public static void main(String args[]) { SecureRandom objSecureRandom = new SecureRandom(); int randomInt1 = objSecureRandom.nextInt(1000); int randonInt2 = objSecureRandom.nextInt(1000); System.out.println("Random Integers: " + randomInt1); System.out.println("Random Integers: " + randonInt2); } }
🌐
Medium
medium.com › @lucideus › secure-random-number-generation-in-java-lucideus-5f5fb5151bde
Secure Random Number Generation in Java | Lucideus | by Lucideus | Medium
December 21, 2018 - Import java.security.SecureRandom · public static int generateRandom(int maximumValue) { SecureRandom ranGen = new SecureRandom(); return ranGen.nextInt(maximumValue); } Tips for Developers while generating Secure Random Number Generators · Don’t use Math.random for random number generation as it is cryptographically insecure.
🌐
Tersesystems
tersesystems.com › blog › 2015 › 12 › 17 › the-right-way-to-use-securerandom
The Right Way to Use SecureRandom · Terse Systems
December 17, 2015 - The nextBytes method is the base method: when you call nextInt or nextLong, etc, it will call down to nextBytes under the hood. The generateSeed method is not needed for a Native PRNG of any type, but it IS useful to seed a user space PRNG such as SHA1PRNG. You can call setSeed on a NativePRNG, ...
🌐
HowToDoInJava
howtodoinjava.com › home › java 8 › secure random number generation in java
Secure Random Number Generation in Java
October 1, 2022 - For information on additional providers, see Java Cryptography Architecture Oracle Providers Documentation for JDK 8. Oracle also provides documentation describing the providers by OS platform. Create a new instance of SecureRandom periodically and reseed, like this:
🌐
Oracle
docs.oracle.com › javase › 10 › docs › api › java › security › SecureRandom.html
SecureRandom (Java SE 10 & JDK 10 )
Otherwise, this class will instead synchronize access to the following methods of the SecureRandomSpi implementation: ... doubles, doubles, doubles, doubles, ints, ints, ints, ints, longs, longs, longs, longs, nextBoolean, nextDouble, nextFloat, nextGaussian, nextInt, nextInt, nextLong