DecimalRandom implements OrderedRandom<Double> { @Override public Double between(final Double lower, final Double upper) { if (lower > upper) { throw new IllegalArgumentException( String.format( "Could not get an element greater than %s and less than %s", lower, upper ) ); } return this.rand.nextDouble() * (upper - lower) + lower; } DecimalRandom(final double min, final double max); @Override Double between(final Double lower, final Double upper); @Override Double greater(final Double lower); @Override Double lower(final Double upper);  }
@Test public void returnsBetweenDouble() { final DecimalRandom rand = new DecimalRandom( DecimalRandomTest.MIN, DecimalRandomTest.MAX ); final Double first = DecimalRandomTest.random(); final Double second = DecimalRandomTest.random(); final Double low = Math.min(first, second); final Double high = Math.max(first, second); MatcherAssert.assertThat( rand.between(low, high), Matchers.allOf( Matchers.greaterThanOrEqualTo(low), Matchers.lessThanOrEqualTo(high) ) ); }
@Test public void returnsBetweenEqualDouble() { final DecimalRandom rand = new DecimalRandom( DecimalRandomTest.MIN, DecimalRandomTest.MAX ); final Double value = DecimalRandomTest.random(); MatcherAssert.assertThat( rand.between(value, value), Matchers.equalTo(value) ); }
@Test public void errorsWhenLowerGreaterThanUpper() { this.thrown.expect(IllegalArgumentException.class); final DecimalRandom rand = new DecimalRandom( DecimalRandomTest.MIN, DecimalRandomTest.MAX ); final Double value = DecimalRandomTest.random(); rand.between(value, value - 1); }