added more methods to RandomNumberGenerator
All checks were successful
Build, test and publish the Quarkus libraries / build (push) Successful in 1m17s

This commit is contained in:
Jorge Bornhausen 2024-11-04 13:18:10 +01:00
parent 7b956dae94
commit 5623f07436
No known key found for this signature in database
GPG key ID: 19DEB64C8905C9FE
4 changed files with 348 additions and 19 deletions

View file

@ -3,9 +3,149 @@ package ch.phoenixtechnologies.quarkus.commons.random;
import java.math.BigInteger;
import java.util.Random;
/**
* This class provides methods to generate random numbers.
* The methods are similar to those in the Random class, but
* this class uses a RandomProvider to obtain the Random
* instance used for generating the random numbers.
*
* @see Random
* @see RandomProvider
*/
public interface RandomNumberGenerator {
BigInteger generate(int numBits);
/**
* Generates a random integer. Uses a Random instance
* obtained from the RandomProvider.
*
* @return a random integer
*/
int generateInt();
BigInteger generate(int numBits, Random random);
/**
* Generates a random integer between 0 (inclusive) and the
* specified bound (exclusive). Uses a Random instance
* obtained from the RandomProvider.
*
* @param bound the upper bound (exclusive). Must be positive.
* @return a random integer
*/
int generateInt(int bound);
/**
* Generates a random integer between the specified origin
* (inclusive) and the specified bound (exclusive). Uses a
* Random instance obtained from the RandomProvider.
*
* @param origin the lower bound (inclusive)
* @param bound the upper bound (exclusive). Must be greater than origin.
* @return a random integer
*/
int generateInt(int origin, int bound);
/**
* Generates a random long. Uses a Random instance obtained
* from the RandomProvider.
*
* @return a random long
*/
long generateLong();
/**
* Generates a random long between 0 (inclusive) and the
* specified bound (exclusive). Uses a Random instance
* obtained from the RandomProvider.
*
* @param bound the upper bound (exclusive). Must be positive.
* @return a random long
*/
long generateLong(long bound);
/**
* Generates a random long between the specified origin
* (inclusive) and the specified bound (exclusive). Uses a
* Random instance obtained from the RandomProvider.
*
* @param origin the lower bound (inclusive)
* @param bound the upper bound (exclusive). Must be greater than origin.
* @return a random long
*/
long generateLong(long origin, long bound);
/**
* Generates a random float between 0.0 (inclusive) and 1.0 (exclusive).
* Uses a Random instance obtained from the RandomProvider.
*
* @return a random float
*/
float generateFloat();
/**
* Generates a random float between 0.0 (inclusive) and the
* specified bound (exclusive). Uses a Random instance obtained
* from the RandomProvider.
*
* @param bound the upper bound (exclusive). Must be positive.
* @return a random float
*/
float generateFloat(float bound);
/**
* Generates a random float between the specified origin (inclusive)
* and the specified bound (exclusive). Uses a Random instance
* obtained from the RandomProvider.
*
* @param origin the lower bound (inclusive)
* @param bound the upper bound (exclusive). Must be greater than origin.
* @return a random float
*/
float generateFloat(float origin, float bound);
/**
* Generates a random double between 0.0 (inclusive) and 1.0 (exclusive).
* Uses a Random instance obtained from the RandomProvider.
*
* @return a random double
*/
double generateDouble();
/**
* Generates a random double between 0.0 (inclusive) and the
* specified bound (exclusive). Uses a Random instance obtained
* from the RandomProvider.
*
* @param bound the upper bound (exclusive). Must be positive.
* @return a random double
*/
double generateDouble(double bound);
/**
* Generates a random double between the specified origin (inclusive)
* and the specified bound (exclusive). Uses a Random instance
* obtained from the RandomProvider.
*
* @param origin the lower bound (inclusive)
* @param bound the upper bound (exclusive). Must be greater than origin.
* @return a random double
*/
double generateDouble(double origin, double bound);
/**
* Generates a random BigInteger with the specified number of bits.
* Uses a Random instance obtained from the RandomProvider.
*
* @param numBits the number of bits of the BigInteger to generate
* @return a random BigInteger
*/
BigInteger generateBigInteger(int numBits);
/**
* Generates a random BigInteger with the specified number of bits
* using the specified Random instance.
*
* @param numBits the number of bits of the BigInteger to generate
* @param random the Random instance to use
* @return a random BigInteger
*/
BigInteger generateBigInteger(int numBits, Random random);
}

View file

@ -17,12 +17,72 @@ class RandomNumberGeneratorImpl implements RandomNumberGenerator {
}
@Override
public BigInteger generate(int numBits) {
return generate(numBits, provider.get());
public int generateInt() {
return provider.get().nextInt();
}
@Override
public BigInteger generate(int numBits, Random random) {
public int generateInt(int bound) {
return provider.get().nextInt(bound);
}
@Override
public int generateInt(int origin, int bound) {
return provider.get().nextInt(origin, bound);
}
@Override
public long generateLong() {
return provider.get().nextLong();
}
@Override
public long generateLong(long bound) {
return provider.get().nextLong(bound);
}
@Override
public long generateLong(long origin, long bound) {
return provider.get().nextLong(origin, bound);
}
@Override
public float generateFloat() {
return provider.get().nextFloat();
}
@Override
public float generateFloat(float bound) {
return provider.get().nextFloat(bound);
}
@Override
public float generateFloat(float origin, float bound) {
return provider.get().nextFloat(origin, bound);
}
@Override
public double generateDouble() {
return provider.get().nextDouble();
}
@Override
public double generateDouble(double bound) {
return provider.get().nextDouble(bound);
}
@Override
public double generateDouble(double origin, double bound) {
return provider.get().nextDouble(origin, bound);
}
@Override
public BigInteger generateBigInteger(int numBits) {
return new BigInteger(numBits, provider.get());
}
@Override
public BigInteger generateBigInteger(int numBits, Random random) {
return new BigInteger(numBits, random);
}
}

View file

@ -21,29 +21,156 @@ class RandomNumberGeneratorImplTest {
RandomProvider provider;
@Test
void generateWithRandomFromProvider() {
void generateInt() {
when(provider.get()).thenReturn(getRandom());
var expected = BigInteger.valueOf(366467854850L);
var actual = generator.generate(40);
var actual = generator.generateInt();
assertThat(actual)
.as("Generated random number should match expected value")
.isEqualTo(expected);
.as("Generated int should match expected value")
.isEqualTo(-1305521323);
}
@Test
void generateWithLocalRandom() {
var expected = BigInteger.valueOf(366467854850L);
void generateIntWithBound() {
when(provider.get()).thenReturn(getRandom());
var random = getRandom();
var actual = generator.generate(40, random);
var actual = generator.generateInt(40);
assertThat(actual)
.as("Generated random number should match expected value")
.isEqualTo(expected);
.as("Generated int should match expected value")
.isEqualTo(26);
}
@Test
void generateIntWithOriginAndBound() {
when(provider.get()).thenReturn(getRandom());
var actual = generator.generateInt(1, 40);
assertThat(actual)
.as("Generated int should match expected value")
.isEqualTo(17);
}
@Test
void generateLong() {
when(provider.get()).thenReturn(getRandom());
var actual = generator.generateLong();
assertThat(actual)
.as("Generated long should match expected value")
.isEqualTo(-5607171386684657918L);
}
@Test
void generateLongWithBound() {
when(provider.get()).thenReturn(getRandom());
var actual = generator.generateLong(40L);
assertThat(actual)
.as("Generated long should match expected value")
.isEqualTo(9L);
}
@Test
void generateLongWithOriginAndBound() {
when(provider.get()).thenReturn(getRandom());
var actual = generator.generateLong(1L, 40L);
assertThat(actual)
.as("Generated long should match expected value")
.isEqualTo(23L);
}
@Test
void generateFloat() {
when(provider.get()).thenReturn(getRandom());
var actual = generator.generateFloat();
assertThat(actual)
.as("Generated float should match expected value")
.isEqualTo(0.6960346f);
}
@Test
void generateFloatWithBound() {
when(provider.get()).thenReturn(getRandom());
var actual = generator.generateFloat(0.99f);
assertThat(actual)
.as("Generated float should match expected value")
.isEqualTo(0.6890743f);
}
@Test
void generateFloatWithOriginAndBound() {
when(provider.get()).thenReturn(getRandom());
var actual = generator.generateFloat(0.01f, 0.99f);
assertThat(actual)
.as("Generated float should match expected value")
.isEqualTo(0.69211394f);
}
@Test
void generateDouble() {
when(provider.get()).thenReturn(getRandom());
var actual = generator.generateDouble();
assertThat(actual)
.as("Generated double should match expected value")
.isEqualTo(0.6960346394874213d);
}
@Test
void generateDoubleWithBound() {
when(provider.get()).thenReturn(getRandom());
var actual = generator.generateDouble(0.99d);
assertThat(actual)
.as("Generated double should match expected value")
.isEqualTo(0.6890742930925471d);
}
@Test
void generateDoubleWithOriginAndBound() {
when(provider.get()).thenReturn(getRandom());
var actual = generator.generateDouble(0.01d, 0.99d);
assertThat(actual)
.as("Generated double should match expected value")
.isEqualTo(0.6921139466976729d);
}
@Test
void generateBigInteger() {
when(provider.get()).thenReturn(getRandom());
var actual = generator.generateBigInteger(40);
assertThat(actual)
.as("Generated BigInteger should match expected value")
.isEqualTo(BigInteger.valueOf(366467854850L));
}
@Test
void generateBigIntegerWithRandomInstance() {
var actual = generator.generateBigInteger(40, getRandom());
assertThat(actual)
.as("Generated BigInteger should match expected value")
.isEqualTo(BigInteger.valueOf(366467854850L));
}
private static Random getRandom() {

View file

@ -6,6 +6,7 @@ import org.junit.jupiter.api.Test;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.SecureRandom;
import java.security.Security;
import static org.assertj.core.api.Assertions.assertThat;
@ -24,7 +25,8 @@ class RandomProviderImplTest {
void get() {
assertThat(provider.get())
.as("Random instance should not be null")
.isNotNull();
.isNotNull()
.isInstanceOf(SecureRandom.class);
}
@Test