MathUtils

This is a utility class with mathematical helper functions.

Methods
static int convertLongToInt(long l)
Convert a long value to an int value.
static int convertLongToInt(long l)
Convert a long value to an int value. Values larger than the biggest int value is converted to the biggest int value, and values smaller than the smallest int value are converted to the smallest int value.
Parameters:
l - the value to convert
Returns:
the converted int value
static byte[] generateAlternativeSeed()
Generate a seed value, using as much unpredictable data as possible.
static byte[] generateAlternativeSeed()
Generate a seed value, using as much unpredictable data as possible.
Returns:
the seed
static int nextPowerOf2(int x)
Get the value that is equal to or higher than this value, and that is a power of two.
static int nextPowerOf2(int x) throws IllegalArgumentException
Get the value that is equal to or higher than this value, and that is a power of two.
Parameters:
x - the original value
Returns:
the next power of two value
Throws:
IllegalArgumentException - if x < 0 or x > 0x40000000
static void randomBytes(byte[] bytes)
Get a number of pseudo random bytes.
static void randomBytes(byte[] bytes)
Get a number of pseudo random bytes.
Parameters:
bytes - the target array
static int randomInt(int lowerThan)
Get a pseudo random int value between 0 (including and the given value (excluding).
static int randomInt(int lowerThan)
Get a pseudo random int value between 0 (including and the given value (excluding). The value is not cryptographically secure.
Parameters:
lowerThan - the value returned will be lower than this value
Returns:
the random long value
static int roundUpInt(int x, int blockSizePowerOf2)
Round the value up to the next block size.
static int roundUpInt(int x, int blockSizePowerOf2)
Round the value up to the next block size. The block size must be a power of two. As an example, using the block size of 8, the following rounding operations are done: 0 stays 0; values 1..8 results in 8, 9..16 results in 16, and so on.
Parameters:
x - the value to be rounded
blockSizePowerOf2 - the block size
Returns:
the rounded value
static long roundUpLong(long x, long blockSizePowerOf2)
Round the value up to the next block size.
static long roundUpLong(long x, long blockSizePowerOf2)
Round the value up to the next block size. The block size must be a power of two. As an example, using the block size of 8, the following rounding operations are done: 0 stays 0; values 1..8 results in 8, 9..16 results in 16, and so on.
Parameters:
x - the value to be rounded
blockSizePowerOf2 - the block size
Returns:
the rounded value
static byte[] secureRandomBytes(int len)
Get a number of cryptographically secure pseudo random bytes.
static byte[] secureRandomBytes(int len)
Get a number of cryptographically secure pseudo random bytes.
Parameters:
len - the number of bytes
Returns:
the random bytes
static int secureRandomInt(int lowerThan)
Get a cryptographically secure pseudo random int value between 0 (including and the given value (excluding).
static int secureRandomInt(int lowerThan)
Get a cryptographically secure pseudo random int value between 0 (including and the given value (excluding).
Parameters:
lowerThan - the value returned will be lower than this value
Returns:
the random long value
static long secureRandomLong()
Get a cryptographically secure pseudo random long value.
static long secureRandomLong()
Get a cryptographically secure pseudo random long value.
Returns:
the random long value
static void warn(String s, Throwable t)
Print a message to system output if there was a problem initializing the random number generator.
static void warn(String s, Throwable t)
Print a message to system output if there was a problem initializing the random number generator.
Parameters:
s - the message to print
t - the stack trace

Fields
static SecureRandom cachedSecureRandom
static boolean seeded

cachedSecureRandom

The secure random object.

seeded

True if the secure random object is seeded.