src/share/classes/java/util/Random.java

Print this page
rev 7727 : 8020539: Clean up doclint problems in java.util package, part 2
Summary: Clean up doclint errors and warnings in classes in java.util
Reviewed-by: darcy,chegar
Contributed-by: Brian Burkhalter <brian.burkhalter@oracle.com>


 208      *          n-- > 0; rnd >>= 8)
 209      *       bytes[i++] = (byte)rnd;
 210      * }}</pre>
 211      *
 212      * @param  bytes the byte array to fill with random bytes
 213      * @throws NullPointerException if the byte array is null
 214      * @since  1.1
 215      */
 216     public void nextBytes(byte[] bytes) {
 217         for (int i = 0, len = bytes.length; i < len; )
 218             for (int rnd = nextInt(),
 219                      n = Math.min(len - i, Integer.SIZE/Byte.SIZE);
 220                  n-- > 0; rnd >>= Byte.SIZE)
 221                 bytes[i++] = (byte)rnd;
 222     }
 223 
 224     /**
 225      * Returns the next pseudorandom, uniformly distributed {@code int}
 226      * value from this random number generator's sequence. The general
 227      * contract of {@code nextInt} is that one {@code int} value is
 228      * pseudorandomly generated and returned. All 2<font size="-1"><sup>32
 229      * </sup></font> possible {@code int} values are produced with
 230      * (approximately) equal probability.
 231      *
 232      * <p>The method {@code nextInt} is implemented by class {@code Random}
 233      * as if by:
 234      *  <pre> {@code
 235      * public int nextInt() {
 236      *   return next(32);
 237      * }}</pre>
 238      *
 239      * @return the next pseudorandom, uniformly distributed {@code int}
 240      *         value from this random number generator's sequence
 241      */
 242     public int nextInt() {
 243         return next(32);
 244     }
 245 
 246     /**
 247      * Returns a pseudorandom, uniformly distributed {@code int} value
 248      * between 0 (inclusive) and the specified value (exclusive), drawn from
 249      * this random number generator's sequence.  The general contract of
 250      * {@code nextInt} is that one {@code int} value in the specified range


 353      *   return next(1) != 0;
 354      * }}</pre>
 355      *
 356      * @return the next pseudorandom, uniformly distributed
 357      *         {@code boolean} value from this random number generator's
 358      *         sequence
 359      * @since 1.2
 360      */
 361     public boolean nextBoolean() {
 362         return next(1) != 0;
 363     }
 364 
 365     /**
 366      * Returns the next pseudorandom, uniformly distributed {@code float}
 367      * value between {@code 0.0} and {@code 1.0} from this random
 368      * number generator's sequence.
 369      *
 370      * <p>The general contract of {@code nextFloat} is that one
 371      * {@code float} value, chosen (approximately) uniformly from the
 372      * range {@code 0.0f} (inclusive) to {@code 1.0f} (exclusive), is
 373      * pseudorandomly generated and returned. All 2<font
 374      * size="-1"><sup>24</sup></font> possible {@code float} values
 375      * of the form <i>m&nbsp;x&nbsp;</i>2<font
 376      * size="-1"><sup>-24</sup></font>, where <i>m</i> is a positive
 377      * integer less than 2<font size="-1"><sup>24</sup> </font>, are
 378      * produced with (approximately) equal probability.
 379      *
 380      * <p>The method {@code nextFloat} is implemented by class {@code Random}
 381      * as if by:
 382      *  <pre> {@code
 383      * public float nextFloat() {
 384      *   return next(24) / ((float)(1 << 24));
 385      * }}</pre>
 386      *
 387      * <p>The hedge "approximately" is used in the foregoing description only
 388      * because the next method is only approximately an unbiased source of
 389      * independently chosen bits. If it were a perfect source of randomly
 390      * chosen bits, then the algorithm shown would choose {@code float}
 391      * values from the stated range with perfect uniformity.<p>
 392      * [In early versions of Java, the result was incorrectly calculated as:
 393      *  <pre> {@code
 394      *   return next(30) / ((float)(1 << 30));}</pre>
 395      * This might seem to be equivalent, if not better, but in fact it
 396      * introduced a slight nonuniformity because of the bias in the rounding
 397      * of floating-point numbers: it was slightly more likely that the




 208      *          n-- > 0; rnd >>= 8)
 209      *       bytes[i++] = (byte)rnd;
 210      * }}</pre>
 211      *
 212      * @param  bytes the byte array to fill with random bytes
 213      * @throws NullPointerException if the byte array is null
 214      * @since  1.1
 215      */
 216     public void nextBytes(byte[] bytes) {
 217         for (int i = 0, len = bytes.length; i < len; )
 218             for (int rnd = nextInt(),
 219                      n = Math.min(len - i, Integer.SIZE/Byte.SIZE);
 220                  n-- > 0; rnd >>= Byte.SIZE)
 221                 bytes[i++] = (byte)rnd;
 222     }
 223 
 224     /**
 225      * Returns the next pseudorandom, uniformly distributed {@code int}
 226      * value from this random number generator's sequence. The general
 227      * contract of {@code nextInt} is that one {@code int} value is
 228      * pseudorandomly generated and returned. All 2<sup>32</sup> possible
 229      * {@code int} values are produced with (approximately) equal probability.

 230      *
 231      * <p>The method {@code nextInt} is implemented by class {@code Random}
 232      * as if by:
 233      *  <pre> {@code
 234      * public int nextInt() {
 235      *   return next(32);
 236      * }}</pre>
 237      *
 238      * @return the next pseudorandom, uniformly distributed {@code int}
 239      *         value from this random number generator's sequence
 240      */
 241     public int nextInt() {
 242         return next(32);
 243     }
 244 
 245     /**
 246      * Returns a pseudorandom, uniformly distributed {@code int} value
 247      * between 0 (inclusive) and the specified value (exclusive), drawn from
 248      * this random number generator's sequence.  The general contract of
 249      * {@code nextInt} is that one {@code int} value in the specified range


 352      *   return next(1) != 0;
 353      * }}</pre>
 354      *
 355      * @return the next pseudorandom, uniformly distributed
 356      *         {@code boolean} value from this random number generator's
 357      *         sequence
 358      * @since 1.2
 359      */
 360     public boolean nextBoolean() {
 361         return next(1) != 0;
 362     }
 363 
 364     /**
 365      * Returns the next pseudorandom, uniformly distributed {@code float}
 366      * value between {@code 0.0} and {@code 1.0} from this random
 367      * number generator's sequence.
 368      *
 369      * <p>The general contract of {@code nextFloat} is that one
 370      * {@code float} value, chosen (approximately) uniformly from the
 371      * range {@code 0.0f} (inclusive) to {@code 1.0f} (exclusive), is
 372      * pseudorandomly generated and returned. All 2<sup>24</sup> possible
 373      * {@code float} values of the form <i>m&nbsp;x&nbsp;</i>2<sup>-24</sup>,
 374      * where <i>m</i> is a positive integer less than 2<sup>24</sup>, are


 375      * produced with (approximately) equal probability.
 376      *
 377      * <p>The method {@code nextFloat} is implemented by class {@code Random}
 378      * as if by:
 379      *  <pre> {@code
 380      * public float nextFloat() {
 381      *   return next(24) / ((float)(1 << 24));
 382      * }}</pre>
 383      *
 384      * <p>The hedge "approximately" is used in the foregoing description only
 385      * because the next method is only approximately an unbiased source of
 386      * independently chosen bits. If it were a perfect source of randomly
 387      * chosen bits, then the algorithm shown would choose {@code float}
 388      * values from the stated range with perfect uniformity.<p>
 389      * [In early versions of Java, the result was incorrectly calculated as:
 390      *  <pre> {@code
 391      *   return next(30) / ((float)(1 << 30));}</pre>
 392      * This might seem to be equivalent, if not better, but in fact it
 393      * introduced a slight nonuniformity because of the bias in the rounding
 394      * of floating-point numbers: it was slightly more likely that the