Initialization and Generating random BigIntegers


BigInteger

The BigInteger class is used for mathematical operations involving large integers with magnitudes too large for primitive data types. For example 100-factorial is 158 digits - much larger than a long can represent. BigInteger provides analogues to all of Java's primitive integer operators, and all relevant methods from java.lang.Math as well as few other operations.


Initialization

The java.math.BigInteger class provides operations analogues to all of Java's primitive integer operators and for all relevant methods from java.lang.Math. As the java.math package is not automatically made available you may have to import java.math.BigInteger before you can use the simple class name.

To convert long or int values to BigInteger use:

long longValue = Long.MAX_VALUE;
BigInteger valueFromLong = BigInteger.valueOf(longValue);

or, for integers:

int intValue = Integer.MIN_VALUE; // negative
BigInteger valueFromInt = BigInteger.valueOf(intValue);

which will widen the intValue integer to long, using sign bit extension for negative values, so that negative values will stay negative.

To convert a numeric String to BigInteger use:

String decimalString = "-1";
BigInteger valueFromDecimalString = new BigInteger(decimalString);

Following constructor is used to translate the String representation of a BigInteger in the specified radix into a BigInteger.

String binaryString = "10";
int binaryRadix = 2;
BigInteger valueFromBinaryString = new BigInteger(binaryString , binaryRadix);

Java also supports direct conversion of bytes to an instance of BigInteger. Currently only signed and unsigned big endian encoding may be used:

byte[] bytes = new byte[] { (byte) 0x80 };
BigInteger valueFromBytes = new BigInteger(bytes);

This will generate a BigInteger instance with value -128 as the first bit is interpreted as the sign bit.

byte[] unsignedBytes = new byte[] { (byte) 0x80 };
int sign = 1; // positive
BigInteger valueFromUnsignedBytes = new BigInteger(sign, unsignedBytes);

This will generate a BigInteger instance with value 128 as the bytes are interpreted as unsigned number, and the sign is explicitly set to 1, a positive number.

There are predefined constants for common values:

  • BigInteger.ZERO — value of "0".
  • BigInteger.ONE — value of "1".
  • BigInteger.TEN — value of "10"

There's also BigInteger.TWO (value of "2"), but you can't use it in your code because it's private


Generating random BigIntegers

The BigInteger class has a constructor dedicated to generate random BigIntegers, given an instance of java.util.Random and an int that specifies how many bits will the BigInteger have. Its usage is quite simple - when you call the constructor BigInteger(int, Random) like this:

BigInteger randomBigInt = new BigInteger(bitCount, sourceOfRandomness);

then you'll end up with a BigInteger whose value is between 0 (inclusive) and 2bitCount (exclusive).

This also means that new BigInteger(2147483647, sourceOfRandomness) may return all positive BigIntegers given enough time.

What will the sourceOfRandomness be is up to you. For example, a new Random() is good enough in most cases:

new BigInteger(32, new Random());

If you're willing to give up speed for higher-quality random numbers, you can use a new ="https://docs.oracle.com/javase/8/docs/api/java/security/SecureRandom.html" rel="nofollow noreferrer">SecureRandom() instead:

import java.security.SecureRandom;
// somewhere in the code...
new BigInteger(32, new SecureRandom());

You can even implement an algorithm on-the-fly with an anonymous class! Note that rolling out your own RNG algorithm will end you up with low quality randomness, so always be sure to use an algorithm that is proven to be decent unless you want the resulting BigInteger(s) to be predictable.

new BigInteger(32, new Random() {
 int seed = 0;
 @Override
 protected int next(int bits) {
 seed = ((22695477 * seed) + 1) & 2147483647; // Values shamelessly stolen from

="https://en.wikipedia.org/wiki/Linear_congruential_generator#Parameters_in_common_use" rel="nofollow noreferrer">Wikipedia return seed; } });

Basic Programs