@Test void nextBigDecimal_withLargeNumbers_givesValuesWithHighPrecision() { BigDecimal lower = Defaults.NUMERIC_MIN.setScale(20); BigDecimal upper = Defaults.NUMERIC_MAX.setScale(20); Stream<String> result = Stream.generate(() -> random.nextBigDecimal(lower, upper)) .map(BigDecimal::toPlainString) .peek(System.out::println) .map(this::lastFourLetters); assertTrue(result.limit(10).noneMatch("0000"::equals)); }
@Override public BigDecimal nextBigDecimal(BigDecimal lowerInclusive, BigDecimal upperExclusive) { int lowerScale = lowerInclusive.scale(); int upperScale = upperExclusive.scale(); int greatestScale = Math.max(lowerScale, upperScale); BigInteger lowerValue = adjustToScale(lowerInclusive, greatestScale); BigInteger upperValue = adjustToScale(upperExclusive, greatestScale); return new BigDecimal(nextBigInteger(lowerValue, upperValue), greatestScale); }
JavaUtilRandomNumberGenerator implements RandomNumberGenerator { @Override public BigDecimal nextBigDecimal(BigDecimal lowerInclusive, BigDecimal upperExclusive) { int lowerScale = lowerInclusive.scale(); int upperScale = upperExclusive.scale(); int greatestScale = Math.max(lowerScale, upperScale); BigInteger lowerValue = adjustToScale(lowerInclusive, greatestScale); BigInteger upperValue = adjustToScale(upperExclusive, greatestScale); return new BigDecimal(nextBigInteger(lowerValue, upperValue), greatestScale); } }
JavaUtilRandomNumberGenerator implements RandomNumberGenerator { @Override public BigDecimal nextBigDecimal(BigDecimal lowerInclusive, BigDecimal upperExclusive) { int lowerScale = lowerInclusive.scale(); int upperScale = upperExclusive.scale(); int greatestScale = Math.max(lowerScale, upperScale); BigInteger lowerValue = adjustToScale(lowerInclusive, greatestScale); BigInteger upperValue = adjustToScale(upperExclusive, greatestScale); return new BigDecimal(nextBigInteger(lowerValue, upperValue), greatestScale); } JavaUtilRandomNumberGenerator();  JavaUtilRandomNumberGenerator(long seed); }
JavaUtilRandomNumberGenerator implements RandomNumberGenerator { @Override public BigDecimal nextBigDecimal(BigDecimal lowerInclusive, BigDecimal upperExclusive) { int lowerScale = lowerInclusive.scale(); int upperScale = upperExclusive.scale(); int greatestScale = Math.max(lowerScale, upperScale); BigInteger lowerValue = adjustToScale(lowerInclusive, greatestScale); BigInteger upperValue = adjustToScale(upperExclusive, greatestScale); return new BigDecimal(nextBigInteger(lowerValue, upperValue), greatestScale); } JavaUtilRandomNumberGenerator();  JavaUtilRandomNumberGenerator(long seed); @Override int nextInt(); @Override int nextInt(int bound); @Override long nextLong(long lowerInclusive, long upperExclusive); @Override double nextDouble(double lowerInclusive, double upperExclusive); @Override BigDecimal nextBigDecimal(BigDecimal lowerInclusive, BigDecimal upperExclusive); }
JavaUtilRandomNumberGenerator implements RandomNumberGenerator { @Override public BigDecimal nextBigDecimal(BigDecimal lowerInclusive, BigDecimal upperExclusive) { int lowerScale = lowerInclusive.scale(); int upperScale = upperExclusive.scale(); int greatestScale = Math.max(lowerScale, upperScale); BigInteger lowerValue = adjustToScale(lowerInclusive, greatestScale); BigInteger upperValue = adjustToScale(upperExclusive, greatestScale); return new BigDecimal(nextBigInteger(lowerValue, upperValue), greatestScale); } JavaUtilRandomNumberGenerator();  JavaUtilRandomNumberGenerator(long seed); @Override int nextInt(); @Override int nextInt(int bound); @Override long nextLong(long lowerInclusive, long upperExclusive); @Override double nextDouble(double lowerInclusive, double upperExclusive); @Override BigDecimal nextBigDecimal(BigDecimal lowerInclusive, BigDecimal upperExclusive);  }