🌐
GitHub
github.com › openjdk › jdk17 › blob › master › src › java.base › share › classes › java › lang › Integer.java
jdk17/src/java.base/share/classes/java/lang/Integer.java at master · openjdk/jdk17
import static java.lang.String.UTF16; · /** * The {@code Integer} class wraps a value of the primitive type · * {@code int} in an object. An object of type {@code Integer} * contains a single field whose type is {@code int}. * * <p>In ...
Author   openjdk
🌐
GitHub
github.com › openjdk › jdk › blob › master › src › java.base › share › classes › java › lang › Integer.java
jdk/src/java.base/share/classes/java/lang/Integer.java at master · openjdk/jdk
JDK main-line development https://openjdk.org/projects/jdk - jdk/src/java.base/share/classes/java/lang/Integer.java at master · openjdk/jdk
Author   openjdk
🌐
GitHub
github.com › io7m-com › jintegers
GitHub - io7m-com/jintegers: Integer handling · GitHub
The jintegers package provides basic functions to pack and unpack integers to/from byte arrays in specific byte orders. As the Java platform evolves, libraries that may have been necessary in the past can become unnecessary due to new platform ...
Author   io7m-com
🌐
GitHub
github.com › openjdk-mirror › jdk7u-jdk › blob › master › src › share › classes › java › lang › Integer.java
jdk7u-jdk/src/share/classes/java/lang/Integer.java at master · openjdk-mirror/jdk7u-jdk
import java.util.Properties; · /** * The {@code Integer} class wraps a value of the primitive type · * {@code int} in an object. An object of type {@code Integer} * contains a single field whose type is {@code int}. * * <p>In addition, ...
Author   openjdk-mirror
🌐
GitHub
github.com › openjdk › jdk › blob › master › src › java.base › share › classes › java › math › BigInteger.java
jdk/src/java.base/share/classes/java/math/BigInteger.java at master · openjdk/jdk
JDK main-line development https://openjdk.org/projects/jdk - jdk/src/java.base/share/classes/java/math/BigInteger.java at master · openjdk/jdk
Author   openjdk
🌐
GitHub
github.com › frohoff › jdk8u-jdk › blob › master › src › share › classes › java › lang › Integer.java
jdk8u-jdk/src/share/classes/java/lang/Integer.java at master · frohoff/jdk8u-jdk
* desired, the {@link java.lang.String#toUpperCase()} method may · * be called on the result: * * <blockquote> * {@code Integer.toString(n, 16).toUpperCase()} * </blockquote> * * @param i an integer to be converted to a string.
Author   frohoff
🌐
Onemancrew
onemancrew.github.io › 2021-11-11-java_17
Java 17, What's New?
November 11, 2021 - The below example uses the new Java 17 RandomGeneratorFactory to get the famous Xoshiro256PlusPlus PRNG algorithms to generate random integers within a specific range, 0 – 10:
🌐
GitHub
github.com › topics › integer
integer · GitHub Topics · GitHub
config java automation generator tool hexadecimal hackintosh clover integer opencore scanpolicy scanpolicy-generator scanpolicy-hex scanpolicy-hackintosh · Updated · Nov 17, 2024 · Java · Star 2 · RSA encryption calculator using java · java cryptography crypto encryption rsa big biginteger integer rsa-cryptography rsa-algorithm big-integer ·
Top answer
1 of 1
1

Just using Random & SecureRandom

Why do you need this? Because the new Random() gets you a relatively fast non-secure random number generator and new SecureRandom() provides a CSPRNG (Cryptographically Secure Pseudo Random Number Generator). Both those instances are instances of the RandomGenerator interface. So the only time you'd need this is if you want a specific Random generator defined in the random package.

If you don't need speed and you haven't any specific needs regarding the distribution - you'd probably know if you'd have any - then please just use new BigInteger(n, new SecureRandom()).

Implementing Random

TL;DR: don't

As RandomGenerator is an interface. The Random class has been refactored into a class that implements the interface. That means you can use the old Random wherever an interface specifies RandomGenerator. However, that doesn't work the other way around.

Random itself is not a final class, so it can be inherited from. So you could, in principle, create an adapter class by simply implementing all methods in Random and calling the similarly named methods of a wrapped RandomGenerator. This is however very dangerous practice as future extensions of Random may break everything. In the worst case it will show inconsistent behavior by mixing the state of the parent Random object and the wrapped RandomGenerator.

Move to Java 19

In Java 19 (build b21) there is an adapter method provided for RandomGenerator named Random#from(RandomGenerator). You can see the feature request here. Note that this is an intermediate step; preferably the BigInteger method should be retrofitted to use RandomGenerator (as stated in the feature request).

Beware that you should never use this wrapper class if you suspect that setSeed can ever be called, e.g. when generating a large prime in the BigInteger class. If it is called it will generate an UnsupportedOperationException.

These kind of classes go through all kinds of testing so the Java provided method can be trusted. However, the fact that a method can be retrofitted to use setSeed could be a problem regarding forward compatibility - you have been warned.

The special case of [0, n^2)

However, you've got one out: the range 0..2^n-1 or [0, 2^n-1) is a number consisting of a set of bits. So what you can do is use your RandomGenerator, fill an array of bytes, remove the most significant bits from the first byte (as Java is big endian), and then convert the array into a signed integer:

/**
 * Mimics the {@link BigInteger#BigInteger(int, Random)} function using a
 * {@link RandomGenerator} instance.
 * 
 * @param numBits maximum bitLength of the new BigInteger.
 * @param rnd     source of randomness to be used in computing the new
 *                BigInteger.
 * @throws IllegalArgumentException {@code numBits} is negative.
 * @see #bitLength()
 */
public static BigInteger generateRandomBigInteger(int numBits, RandomGenerator rng) {
    if (numBits < 0) {
        throw new IllegalArgumentException("numBits must be non-negative");
    }

    if (numBits == 0) {
        return BigInteger.ZERO;
    }

    int bytes = (numBits + Byte.SIZE - 1) / Byte.SIZE;
    // mask bits that we need the value of to 1, the others - if any -- will be set
    // to zero
    byte bitMask = (byte) ((1 << ((numBits - 1) % Byte.SIZE + 1)) - 1);
    byte[] randomBytes = new byte[bytes];
    rng.nextBytes(randomBytes);
    randomBytes[0] &= bitMask;
    return new BigInteger(1, randomBytes);
}

numBits is of course the same as your n variable.

The case for [0, max)

Actually, you can combine the comparison function and the random bit generator in such a way that you can create large random values in the range 0..max with max having any value, with a minimum amount of random bits and no nasty BigInteger operations (such as division) at all.

You can find my implementation of that here but you may get weirded out. Sorry in advance, and yes, this is a shameless plug of my RNGBC algorithm :)

Find elsewhere
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › lang › Integer.html
Integer (Java SE 17 & JDK 17)
April 21, 2026 - The characters in the string must all be decimal digits, except that the first character may be an ASCII plus sign '+' ('\u002B'). The resulting integer value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseUnsignedInt(java.lang.String, int) method.
🌐
GitHub
github.com › topics › integer-arithmetic
integer-arithmetic · GitHub Topics · GitHub
All 56 C++ 17 Python 8 C 6 Java 3 VHDL 3 Haskell 2 JavaScript 2 Prolog 2 Rust 2 Swift 2 · Star 18 · Optimized integer division for Java · java performance integer-arithmetic division divide-operation · Updated · Oct 13, 2020 · Java · Star 0 · Integer Arithmetic Assignment for 2WF90 ·
🌐
GitHub
github.com › IntegerLimit
IntegerLimit (Integer Limit) · GitHub
IntegerLimit has 45 repositories available. Follow their code on GitHub.
🌐
GitHub
github.com › topics › biginteger
biginteger · GitHub Topics
It has support optional randomisation has a wide support for various Java types including long, UUID and BigInteger. hashids obfuscation uuid serializer jax-rs id jackson hmac hkdf biginteger aes-encryption long integer byte-array hashid database-ids 64bit paramconverter
🌐
GitHub
github.com › topics › java-17
java-17 · GitHub Topics · GitHub
Java 17 and Spring-Boot examples and demo projects.
🌐
GitHub
github.com › gkovan › java17
GitHub - gkovan/java17
Contribute to gkovan/java17 development by creating an account on GitHub.
Author   gkovan
🌐
GitHub
github.com › protocolbuffers › protobuf › issues › 8449
Deprecation warnings when compiling on JDK > 9: Integer(int) in Integer has been deprecated and marked for removal · Issue #8449 · protocolbuffers/protobuf
April 4, 2021 - Deprecation warnings when compiling on JDK > 9: Integer(int) in Integer has been deprecated and marked for removal#8449
Author   protocolbuffers
🌐
GitHub
github.com › topics › digits
Build software better, together
DoubDabC is a Java library that supports binary integer value to decimal sequence conversion with alternative algorithm.
🌐
GitHub
github.com › AdoptOpenJDK › openjdk-jdk11 › blob › master › src › java.base › share › classes › java › util › concurrent › atomic › AtomicInteger.java
openjdk-jdk11/src/java.base/share/classes/java/util/concurrent/atomic/AtomicInteger.java at master · AdoptOpenJDK/openjdk-jdk11
March 2, 2019 - * used as a replacement for an {@link java.lang.Integer}. However, * this class does extend {@code Number} to allow uniform access by · * tools and utilities that deal with numerically-based classes. * * @since 1.5 · * @author Doug Lea · */ public class AtomicInteger extends Number implements java.io.Serializable { private static final long serialVersionUID = 6214790243416807050L; ·
Author   AdoptOpenJDK