Hi. Le ven. 17 janv. 2020 à 13:15, Alex Herbert <alex.d.herb...@gmail.com> a écrit : > > The UniformRandomProvider and ContinuousUniformSampler can create > doubles uniformly in a range [0, 1) and [x, y). > > I would like to create a sampler that can create doubles with random > mantissa bits and a specified range of exponents. These would not follow > a standard distribution but would be distributed according to the IEEE > 754 floating-point "double format" bit layout. > > The sampler would ensure all bits of the mantissa are uniformly sampled > and the exponent range is uniformly sampled. It could be used for > example to create random double data in a specified range for testing.
Are there use-cases other than providing inputs for unit tests? > > Following from UniformRandomProvider I propose to remove the sign bit to > allow a maximum range sampler from [0, Double.MAX_VALUE]. Is there a trade-off to allowing generation of the whole range of "double"s? > Thus we have > the API: > > public final class FiniteDoubleSampler { > > /** > * Creates a new finite double sampler. > * > * <p>This will return double values from the entire exponent range of > a finite > * {@code double} including sub-normal numbers. The value will be unsigned. > * > * @param rng Generator of uniformly distributed random numbers. > * @return Sampler. > * @since 1.4 > */ > public static SharedStateContinuousSampler of(UniformRandomProvider rng); > > /** > * Creates a new finite double sampler. > * > * <p>This will return double values from the specified exponent range > of a finite > * {@code double}. This assumes all sub-normal numbers are identified > with the exponent -1023. > * The value will be unsigned. > * > * @param rng Generator of uniformly distributed random numbers. > * @param minExponent Minimum exponent > * @param maxExponent Maximum exponent > * @see Double#MIN_EXPONENT > * @see Double#MAX_EXPONENT > * @throws IllegalArgumentException If the exponents are not in the > range -1023 inclusive > * to 1023 inclusive; or the min exponent is not {@code <=} max exponent. > * @return Sampler. > * @since 1.4 > */ > public static SharedStateContinuousSampler of(UniformRandomProvider rng, > int minExponent, > int maxExponent); > } > > I have written many tests where I wanted full precision random mantissas > in double values and wanted the doubles to represent all doubles that > could occur. How do you ensure it (short of listing them all)? > Thus randomly sampling from the IEEE 754 representation > seems to be more generic than for example rng.nextDouble() * constant. What's the advantage of the former over the latter? > For example: > > // Random numbers: [0, Double.MAX_VALUE] > FiniteDoubleSampler.of(rng); > FiniteDoubleSampler.of(rng, -1023, 1023); > // Random sub-normal numbers: [0, Double.MIN_NORMAL) > FiniteDoubleSampler.of(rng, -1023, -1023); > // Random numbers that are close to overflow: (Double.MAX_VALUE/2, > Double.MAX_VALUE] > FiniteDoubleSampler.of(rng, 1023, 1023); > // Random numbers in the range [1, 32) > FiniteDoubleSampler.of(rng, 0, 4); Will the distribution be the same as for "32 * rng.nextDouble()"? Regards, Gilles > Thoughts on this? > > Alex > --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org For additional commands, e-mail: dev-h...@commons.apache.org