### Question:
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);  }### Answer:
@Test void nextBigDecimal_withPositiveSameScale_works() { BigDecimal lower = BigDecimal.ONE; BigDecimal upper = BigDecimal.valueOf(3); Set<BigDecimal> expected = SetUtils.setOf(BigDecimal.ONE, BigDecimal.valueOf(2)); assertTrue(Stream.generate(() -> random.nextBigDecimal(lower, upper)) .limit(10) .allMatch(expected::contains)); }
@Test void nextBigDecimal_withPositiveDifferentScales_work() { BigDecimal lower = BigDecimal.valueOf(9); BigDecimal upper = BigDecimal.valueOf(12); Set<BigDecimal> expected = SetUtils.setOf( BigDecimal.valueOf(9), BigDecimal.valueOf(10), BigDecimal.valueOf(11)); assertTrue(Stream.generate(() -> random.nextBigDecimal(lower, upper)) .limit(10) .allMatch(expected::contains)); }
@Test void nextBigDecimal_withNegativeSameScale_works() { BigDecimal lower = new BigDecimal("0.1"); BigDecimal upper = new BigDecimal("0.4"); Set<BigDecimal> expected = SetUtils.setOf(lower, new BigDecimal("0.2"), new BigDecimal("0.3")); assertTrue(Stream.generate(() -> random.nextBigDecimal(lower, upper)) .limit(10) .allMatch(expected::contains)); }
@Test void nextBigDecimal_withNegativeDifferentScales_work() { BigDecimal lower = new BigDecimal("0.08"); BigDecimal upper = new BigDecimal("0.1"); Set<BigDecimal> expected = SetUtils.setOf(lower, new BigDecimal("0.09")); assertTrue(Stream.generate(() -> random.nextBigDecimal(lower, upper)) .limit(10) .allMatch(expected::contains)); }
@Test void nextBigDecimal_withPositiveAndNegativeScales_works() { BigDecimal lower = new BigDecimal("0.9"); BigDecimal upper = BigDecimal.TEN; assertTrue(Stream.generate(() -> random.nextBigDecimal(lower, upper)) .limit(10) .allMatch(x -> BigDecimal.TEN.compareTo(x) > 0 && lower.compareTo(x) <= 0)); }
@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)); }