brainfrz

Untitled

Jan 23rd, 2016
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.18 KB | None | 0 0
  1.     /**
  2.      * Returns a {@code double} value with a positive sign, greater
  3.      * than or equal to {@code 0.0} and less than {@code 1.0}.
  4.      * Returned values are chosen pseudorandomly with (approximately)
  5.      * uniform distribution from that range.
  6.      *
  7.      * <p>When this method is first called, it creates a single new
  8.      * pseudorandom-number generator, exactly as if by the expression
  9.      *
  10.      * <blockquote>{@code new java.util.Random()}</blockquote>
  11.      *
  12.      * This new pseudorandom-number generator is used thereafter for
  13.      * all calls to this method and is used nowhere else.
  14.      *
  15.      * <p>This method is properly synchronized to allow correct use by
  16.      * more than one thread. However, if many threads need to generate
  17.      * pseudorandom numbers at a great rate, it may reduce contention
  18.      * for each thread to have its own pseudorandom-number generator.
  19.      *
  20.      * @return  a pseudorandom {@code double} greater than or equal
  21.      * to {@code 0.0} and less than {@code 1.0}.
  22.      * @see Random#nextDouble()
  23.      */
  24.     public static double random() {
  25.         return RandomNumberGeneratorHolder.randomNumberGenerator.nextDouble();
  26.     }
  27.  
  28.     /**
  29.      * Returns the next pseudorandom, uniformly distributed
  30.      * {@code double} value between {@code 0.0} and
  31.      * {@code 1.0} from this random number generator's sequence.
  32.      *
  33.      * <p>The general contract of {@code nextDouble} is that one
  34.      * {@code double} value, chosen (approximately) uniformly from the
  35.      * range {@code 0.0d} (inclusive) to {@code 1.0d} (exclusive), is
  36.      * pseudorandomly generated and returned.
  37.      *
  38.      * <p>The method {@code nextDouble} is implemented by class {@code Random}
  39.      * as if by:
  40.      *  <pre> {@code
  41.      * public double nextDouble() {
  42.      *   return (((long)next(26) << 27) + next(27))
  43.      *     / (double)(1L << 53);
  44.      * }}</pre>
  45.      *
  46.      * <p>The hedge "approximately" is used in the foregoing description only
  47.      * because the {@code next} method is only approximately an unbiased
  48.      * source of independently chosen bits. If it were a perfect source of
  49.      * randomly chosen bits, then the algorithm shown would choose
  50.      * {@code double} values from the stated range with perfect uniformity.
  51.      * <p>[In early versions of Java, the result was incorrectly calculated as:
  52.      *  <pre> {@code
  53.      *   return (((long)next(27) << 27) + next(27))
  54.      *     / (double)(1L << 54);}</pre>
  55.      * This might seem to be equivalent, if not better, but in fact it
  56.      * introduced a large nonuniformity because of the bias in the rounding
  57.      * of floating-point numbers: it was three times as likely that the
  58.      * low-order bit of the significand would be 0 than that it would be 1!
  59.      * This nonuniformity probably doesn't matter much in practice, but we
  60.      * strive for perfection.]
  61.      *
  62.      * @return the next pseudorandom, uniformly distributed {@code double}
  63.      *         value between {@code 0.0} and {@code 1.0} from this
  64.      *         random number generator's sequence
  65.      * @see Math#random
  66.      */
  67.     public double nextDouble() {
  68.         return (((long)(next(26)) << 27) + next(27)) * DOUBLE_UNIT;
  69.     }
Advertisement
Add Comment
Please, Sign In to add comment