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.

Answer from Stephen C on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › security › SecureRandom.html
SecureRandom (Java Platform SE 8 )
1 week ago - Java™ Platform Standard Ed. 8 ... This class provides a cryptographically strong random number generator (RNG). A cryptographically strong random number minimally complies with the statistical random number generator tests specified in FIPS 140-2, Security Requirements for Cryptographic Modules, ...
🌐
Java Mex
javamex.com › tutorials › random_numbers › securerandom.shtml
The Java SecureRandom class
In order to provide this property of choosing any seed with "equal" likelihood, (or at least, with no bias that is practically detectable), SecureRandom seeds itself from sources of entropy available from the local machine, such as timings of I/O events. 1. Note that with a quantum computer, the complexity of guessing the key is apparently proportional to the square root of the number of possible keys. Then, guessing the 128-bit key at the same rate would take only about 200 days. If you enjoy this Java programming article, please share with friends and colleagues.
🌐
Baeldung
baeldung.com › home › security › the java securerandom class
The Java SecureRandom Class | Baeldung
February 20, 2025 - In this short tutorial, we’ll learn about java.security.SecureRandom, a class that provides a cryptographically strong random number generator.
🌐
Jsparrow
jsparrow.github.io › rules › use-secure-random.html
Use SecureRandom | jSparrow Documentation
An attacker might be able to guess the next value generated by a pseudo-random number generator and thus access sensitive data. A more secure alternative is the class java.security.SecureRandom (opens new window) which relies on a cryptographically strong random number generator (RNG).
🌐
Oracle
docs.oracle.com › cd › E17802_01 › j2se › j2se › 1.5.0 › jcp › beta1 › apidiffs › java › security › SecureRandom.html
java.security Class SecureRandom
Like other algorithm-based classes in Java Security, SecureRandom provides implementation-independent algorithms, whereby a caller (application code) requests a particular RNG algorithm and is handed back a SecureRandom object for that algorithm. It is also possible, if desired, to request a particular algorithm from a particular provider.
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.

🌐
Microej
repository.microej.com › javadoc › microej_5.x › libraries › ssl-2.1-api › java › security › SecureRandom.html
SecureRandom
Many SecureRandom implementations are in the form of a pseudo-random number generator (PRNG), which means they use a deterministic algorithm to produce a pseudo-random sequence from a true random seed.
Find elsewhere
🌐
Metebalci
metebalci.com › blog › everything-about-javas-securerandom
Everything about Java's SecureRandom
November 28, 2018 - For Solaris/Linux/MacOS, it obtains seed and random numbers from /dev/random and /dev/urandom and reads securerandom.source Security property and java.security.egd System property.
🌐
GeeksforGeeks
geeksforgeeks.org › java › securerandom-getinstance-method-in-java-with-examples
SecureRandom getInstance() method in Java with Examples - GeeksforGeeks
June 8, 2020 - A new SecureRandom object encapsulating the SecureRandomSpi implementation from the first Provider that supports the specified algorithm is returned.
🌐
TutorialsPoint
tutorialspoint.com › threadlocalrandom-vs-securerandom-class-in-java
ThreadLocalRandom vs SecureRandom Class in Java
June 19, 2023 - To achieve this, SecureRandom uses a cryptographically strong algorithm that makes the output suitable for security-sensitive operations, such as generating encryption keys or random password generation. ... import java.security.SecureRandom; SecureRandom secureRandom = new SecureRandom(); // Generate a random integer int randomInt = secureRandom.nextInt();
🌐
Stack Overflow
stackoverflow.com › questions › tagged › rack
Newest 'rack' Questions - Stack Overflow
When I add plugin: Cuba.plugin Cuba::Safe In the post method I see that csrf.safe? returns false class Cuba module Safe module CSRF def token session[:csrf_token] ||= SecureRandom....
🌐
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
* "{@docRoot}/../specs/security/standard-names.html#securerandom-number-generation-algorithms"> * Java Security Standard Algorithm Names Specification</a> * for information about standard RNG algorithm names. * @spec security/standard-names.html Java Security Standard Algorithm Names ·
Author   openjdk
🌐
Cach3
tutorialspoint.com.cach3.com › programming_example › r5mI0e › java-securerandom-number-generation.html
Java SecureRandom number Generation - Cach3
Java NIO, PyTorch, SLF4J, Parallax Scrolling, Java Cryptography, YAML, Python Data Science, Java i18n, GitLab, TestRail, VersionOne, DBUtils, Common CLI, Seaborn, Ansible, LOLCODE, Current Affairs 2018, Intellij Idea, Apache Commons Collections, Java 9, GSON, TestLink, Inter Process Communication ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › securerandom-nextbytes-method-in-java-with-examples
SecureRandom nextBytes() method in Java with Examples - GeeksforGeeks
December 4, 2018 - The nextBytes() method of java.security.SecureRandom class is used to generate a user-specified number of random bytes. If a call to setSeed had not occurred previously, the first call to this method forces this SecureRandom object to seed itself.
🌐
Darakian
darakian.github.io › 2019 › 02 › 17 › on-java-securerandom.html
An odd feature of java’s secure random
February 17, 2019 - A SecureRandom is a thread safe, but fully independent random number generator to be used when you care about your numbers being unguessable. If you read the javadoc material alone you’d be forgiven in thinking that all SecureRandom objects share a JVM provided entropy pool.
🌐
The Backend Pro
thebackendpro.com › home › java › difference between random, securerandom and threadlocalrandom
Difference between Random, SecureRandom and ThreadLocalRandom
February 12, 2023 - Here is an example of how to generate random numbers using SecureRandom class. package random; import java.security.SecureRandom; public class SecureRandomTest { public static void main(String[] args) { SecureRandom secureRandom = new SecureRandom(); int randomInt = secureRandom.nextInt(); System.out.println("Random number: " + randomInt); } }
🌐
Uplup
uplup.com › random-name-picker
Spin the Wheel of Names – Free Random Name Picker Tool
Our REST API works with any language that can make HTTP requests. We provide official code examples and SDKs for: JavaScript/Node.js (with async/await support), Python (requests library), PHP (cURL and Guzzle), Ruby (Net::HTTP), Go (net/http), cURL (command line), and community libraries for Java, C#, and more.
Top answer
1 of 1
4

tl;dr

Integer.toHexString(
    new SecureRandom().nextInt()
)

bd594090

JRuby

Run your Ruby code on a JVM with JRuby.

java.security.SecureRandom

Java includes SecureRandom, a cryptographically strong random number generator (RNG).

Pass the number of bits to be generated, right justified, with leading zeros.

int x = new SecureRandom().nextInt() ;

1286623117

To generate a hex string, pass that int to Integer.toHexString.

String output = Integer.toHexString( x ) ;  // And maybe call `toLowerCase` or `toUpperCase` as you desire.

4cb04f8d

Extract from UUID Version 4

A UUID is a 128-bit value designed to be used as a universally-unique identifier. Originally specified as a point in time and space, combining the current moment with a MAC address plus an arbitrary number.

Later Version 4 UUID was defined to randomly generate 122 of the 128 bits. The java.util.UUID class in Java promises to use a cryptographically-strong pseudo-random number generator.

The canonical expression of a UUID is 32 hex characters with 4 hyphens. So if take such a string, remove the hyphens, we can take hex characters at the leading end as the non-random 4 bits are in the second half of the hex string.

For your 32-bit hex value, we need 1/4 of the hex characters (1/4 of 128 bits = 32 bits), or the first 8 hex characters.

String hex = 
    java.util.UUID
    .randomUUID()
    .toString()
    .replace( "-" , "" )  // Remove hyphens, leaving only hex characters.
    .substring( 0 , 8 )   // Annoying zero-based counting. 
;

The UUID standards require the hex string to be lowercase, though many implementation violate this rule. Either way, you should make call to enforce your own choice of uppercase or lowercase hex.

String hex = 
    java.util.UUID
    .randomUUID()
    .toString()
    .replace( "-" , "" )  // Remove hyphens, leaving only hex characters.
    .substring( 0 , 8 )   // Annoying zero-based counting. 
    .toLowerCase()        // Or `toUpperCase` as you desire.
;

Use entire UUID

If your purpose is to generate distinct values as identifiers, consider using the entir universally unique identifier (UUID) value discussed above.

Version 1 UUIDs are best, but if unavailable you can fall back to using Version 4. That is good enough for many purposes.

In Java, see the UUID class.

🌐
GeeksforGeeks
geeksforgeeks.org › java › random-vs-secure-random-numbers-java
Random vs Secure Random numbers in Java - GeeksforGeeks
July 23, 2025 - Prerequisite: Generating Random numbers in Java java.security.SecureRandom class: This class provides a cryptographically strong random number generator (RNG). A cryptographically strong random number minimally complies with the statistical ...
🌐
Android Developers
developer.android.com › api reference › securerandom
SecureRandom | API reference | Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体