@@ -145,7 +145,7 @@ public static void checkStreamSize(long streamSize) {
145
145
* @throws IllegalArgumentException if {@code bound} fails to be positive and finite
146
146
*/
147
147
public static void checkBound (float bound ) {
148
- if (!(bound > 0.0 && bound < Float .POSITIVE_INFINITY )) {
148
+ if (!(0.0f < bound && bound < Float .POSITIVE_INFINITY )) {
149
149
throw new IllegalArgumentException (BAD_FLOATING_BOUND );
150
150
}
151
151
}
@@ -158,7 +158,7 @@ public static void checkBound(float bound) {
158
158
* @throws IllegalArgumentException if {@code bound} fails to be positive and finite
159
159
*/
160
160
public static void checkBound (double bound ) {
161
- if (!(bound > 0.0 && bound < Double .POSITIVE_INFINITY )) {
161
+ if (!(0.0 < bound && bound < Double .POSITIVE_INFINITY )) {
162
162
throw new IllegalArgumentException (BAD_FLOATING_BOUND );
163
163
}
164
164
}
@@ -195,11 +195,13 @@ public static void checkBound(long bound) {
195
195
* @param origin the least value (inclusive) in the range
196
196
* @param bound the upper bound (exclusive) of the range
197
197
*
198
- * @throws IllegalArgumentException if {@code origin} is not finite, {@code bound} is not finite,
199
- * or {@code bound - origin} is not finite
198
+ * @throws IllegalArgumentException if {@code origin} is not finite,
199
+ * or {@code bound} is not finite, or {@code origin}
200
+ * is greater than or equal to {@code bound}
200
201
*/
201
202
public static void checkRange (float origin , float bound ) {
202
- if (!(origin < bound && (bound - origin ) < Float .POSITIVE_INFINITY )) {
203
+ if (!(Float .NEGATIVE_INFINITY < origin && origin < bound &&
204
+ bound < Float .POSITIVE_INFINITY )) {
203
205
throw new IllegalArgumentException (BAD_RANGE );
204
206
}
205
207
}
@@ -210,11 +212,13 @@ public static void checkRange(float origin, float bound) {
210
212
* @param origin the least value (inclusive) in the range
211
213
* @param bound the upper bound (exclusive) of the range
212
214
*
213
- * @throws IllegalArgumentException if {@code origin} is not finite, {@code bound} is not finite,
214
- * or {@code bound - origin} is not finite
215
+ * @throws IllegalArgumentException if {@code origin} is not finite,
216
+ * or {@code bound} is not finite, or {@code origin}
217
+ * is greater than or equal to {@code bound}
215
218
*/
216
219
public static void checkRange (double origin , double bound ) {
217
- if (!(origin < bound && (bound - origin ) < Double .POSITIVE_INFINITY )) {
220
+ if (!(Double .NEGATIVE_INFINITY < origin && origin < bound &&
221
+ bound < Double .POSITIVE_INFINITY )) {
218
222
throw new IllegalArgumentException (BAD_RANGE );
219
223
}
220
224
}
@@ -343,8 +347,8 @@ public static int[] convertSeedBytesToInts(byte[] seed, int n, int z) {
343
347
* This is the form of {@link RandomGenerator#nextLong() nextLong}() used by
344
348
* a {@link LongStream} {@link Spliterator} and by the public method
345
349
* {@link RandomGenerator#nextLong(long, long) nextLong}(origin, bound). If
346
- * {@code origin} is greater than {@code bound}, then this method simply
347
- * calls the unbounded version of
350
+ * {@code origin} is greater than or equal to {@code bound},
351
+ * then this method simply calls the unbounded version of
348
352
* {@link RandomGenerator#nextLong() nextLong}(), choosing pseudorandomly
349
353
* from among all 2<sup>64</sup> possible {@code long} values}, and
350
354
* otherwise uses one or more calls to
@@ -508,8 +512,8 @@ public static long boundedNextLong(RandomGenerator rng, long bound) {
508
512
* This is the form of {@link RandomGenerator#nextInt() nextInt}() used by
509
513
* an {@link IntStream} {@link Spliterator} and by the public method
510
514
* {@link RandomGenerator#nextInt(int, int) nextInt}(origin, bound). If
511
- * {@code origin} is greater than {@code bound}, then this method simply
512
- * calls the unbounded version of
515
+ * {@code origin} is greater than or equal to {@code bound},
516
+ * then this method simply calls the unbounded version of
513
517
* {@link RandomGenerator#nextInt() nextInt}(), choosing pseudorandomly
514
518
* from among all 2<sup>64</sup> possible {@code int} values}, and otherwise
515
519
* uses one or more calls to {@link RandomGenerator#nextInt() nextInt}() to
@@ -604,8 +608,8 @@ public static int boundedNextInt(RandomGenerator rng, int bound) {
604
608
* used by a {@link DoubleStream} {@link Spliterator} and by the public
605
609
* method
606
610
* {@link RandomGenerator#nextDouble(double, double) nextDouble}(origin, bound).
607
- * If {@code origin} is greater than {@code bound}, then this method simply
608
- * calls the unbounded version of
611
+ * {@code origin} is greater than or equal to {@code bound},
612
+ * then this method simply calls the unbounded version of
609
613
* {@link RandomGenerator#nextDouble() nextDouble}(), and otherwise scales
610
614
* and translates the result of a call to
611
615
* {@link RandomGenerator#nextDouble() nextDouble}() so that it lies between
@@ -643,9 +647,15 @@ public static int boundedNextInt(RandomGenerator rng, int bound) {
643
647
public static double boundedNextDouble (RandomGenerator rng , double origin , double bound ) {
644
648
double r = rng .nextDouble ();
645
649
if (origin < bound ) {
646
- r = r * (bound - origin ) + origin ;
650
+ if (bound - origin < Double .POSITIVE_INFINITY ) {
651
+ r = r * (bound - origin ) + origin ;
652
+ } else {
653
+ /* avoids overflow at the cost of 3 more multiplications */
654
+ double halfOrigin = 0.5 * origin ;
655
+ r = (r * (0.5 * bound - halfOrigin ) + halfOrigin ) * 2.0 ;
656
+ }
647
657
if (r >= bound ) // may need to correct a rounding problem
648
- r = Math .nextAfter (bound , origin );
658
+ r = Math .nextDown (bound );
649
659
}
650
660
return r ;
651
661
}
@@ -686,8 +696,8 @@ public static double boundedNextDouble(RandomGenerator rng, double bound) {
686
696
* by a {@link Stream<Float>} {@link Spliterator} (if there were any) and by
687
697
* the public method
688
698
* {@link RandomGenerator#nextFloat(float, float) nextFloat}(origin, bound).
689
- * If {@code origin} is greater than {@code bound}, then this method simply
690
- * calls the unbounded version of
699
+ * {@code origin} is greater than or equal to {@code bound},
700
+ * then this method simply calls the unbounded version of
691
701
* {@link RandomGenerator#nextFloat() nextFloat}(), and otherwise scales and
692
702
* translates the result of a call to
693
703
* {@link RandomGenerator#nextFloat() nextFloat}() so that it lies between
@@ -715,9 +725,15 @@ public static double boundedNextDouble(RandomGenerator rng, double bound) {
715
725
public static float boundedNextFloat (RandomGenerator rng , float origin , float bound ) {
716
726
float r = rng .nextFloat ();
717
727
if (origin < bound ) {
718
- r = r * (bound - origin ) + origin ;
728
+ if (bound - origin < Float .POSITIVE_INFINITY ) {
729
+ r = r * (bound - origin ) + origin ;
730
+ } else {
731
+ /* avoids overflow at the cost of 3 more multiplications */
732
+ float halfOrigin = 0.5f * origin ;
733
+ r = (r * (0.5f * bound - halfOrigin ) + halfOrigin ) * 2.0f ;
734
+ }
719
735
if (r >= bound ) // may need to correct a rounding problem
720
- r = Float . intBitsToFloat ( Float . floatToIntBits ( bound ) - 1 );
736
+ r = Math . nextDown ( r );
721
737
}
722
738
return r ;
723
739
}
@@ -749,7 +765,7 @@ public static float boundedNextFloat(RandomGenerator rng, float bound) {
749
765
float r = rng .nextFloat ();
750
766
r = r * bound ;
751
767
if (r >= bound ) // may need to correct a rounding problem
752
- r = Float . intBitsToFloat ( Float . floatToIntBits ( bound ) - 1 );
768
+ r = Math . nextDown ( r );
753
769
return r ;
754
770
}
755
771
0 commit comments