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

Print this page
rev 7009 : 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
Contributed-by: akhil.arora@oracle.com, brian.goetz@oracle.com


   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util;
  27 import java.io.*;
  28 import java.util.concurrent.atomic.AtomicLong;




  29 import sun.misc.Unsafe;
  30 
  31 /**
  32  * An instance of this class is used to generate a stream of
  33  * pseudorandom numbers. The class uses a 48-bit seed, which is
  34  * modified using a linear congruential formula. (See Donald Knuth,
  35  * <i>The Art of Computer Programming, Volume 2</i>, Section 3.2.1.)
  36  * <p>
  37  * If two instances of {@code Random} are created with the same
  38  * seed, and the same sequence of method calls is made for each, they
  39  * will generate and return identical sequences of numbers. In order to
  40  * guarantee this property, particular algorithms are specified for the
  41  * class {@code Random}. Java implementations must use all the algorithms
  42  * shown here for the class {@code Random}, for the sake of absolute
  43  * portability of Java code. However, subclasses of class {@code Random}
  44  * are permitted to use other algorithms, so long as they adhere to the
  45  * general contracts for all the methods.
  46  * <p>
  47  * The algorithms implemented by class {@code Random} use a
  48  * {@code protected} utility method that on each invocation can supply


 493      *         standard deviation {@code 1.0} from this random number
 494      *         generator's sequence
 495      */
 496     synchronized public double nextGaussian() {
 497         // See Knuth, ACP, Section 3.4.1 Algorithm C.
 498         if (haveNextNextGaussian) {
 499             haveNextNextGaussian = false;
 500             return nextNextGaussian;
 501         } else {
 502             double v1, v2, s;
 503             do {
 504                 v1 = 2 * nextDouble() - 1; // between -1 and 1
 505                 v2 = 2 * nextDouble() - 1; // between -1 and 1
 506                 s = v1 * v1 + v2 * v2;
 507             } while (s >= 1 || s == 0);
 508             double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s)/s);
 509             nextNextGaussian = v2 * multiplier;
 510             haveNextNextGaussian = true;
 511             return v1 * multiplier;
 512         }





















































 513     }
 514 
 515     /**
 516      * Serializable fields for Random.
 517      *
 518      * @serialField    seed long
 519      *              seed for random computations
 520      * @serialField    nextNextGaussian double
 521      *              next Gaussian to be returned
 522      * @serialField      haveNextNextGaussian boolean
 523      *              nextNextGaussian is valid
 524      */
 525     private static final ObjectStreamField[] serialPersistentFields = {
 526         new ObjectStreamField("seed", Long.TYPE),
 527         new ObjectStreamField("nextNextGaussian", Double.TYPE),
 528         new ObjectStreamField("haveNextNextGaussian", Boolean.TYPE)
 529     };
 530 
 531     /**
 532      * Reconstitute the {@code Random} instance from a stream (that is,




   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util;
  27 import java.io.*;
  28 import java.util.concurrent.atomic.AtomicLong;
  29 import java.util.stream.DoubleStream;
  30 import java.util.stream.IntStream;
  31 import java.util.stream.LongStream;
  32 
  33 import sun.misc.Unsafe;
  34 
  35 /**
  36  * An instance of this class is used to generate a stream of
  37  * pseudorandom numbers. The class uses a 48-bit seed, which is
  38  * modified using a linear congruential formula. (See Donald Knuth,
  39  * <i>The Art of Computer Programming, Volume 2</i>, Section 3.2.1.)
  40  * <p>
  41  * If two instances of {@code Random} are created with the same
  42  * seed, and the same sequence of method calls is made for each, they
  43  * will generate and return identical sequences of numbers. In order to
  44  * guarantee this property, particular algorithms are specified for the
  45  * class {@code Random}. Java implementations must use all the algorithms
  46  * shown here for the class {@code Random}, for the sake of absolute
  47  * portability of Java code. However, subclasses of class {@code Random}
  48  * are permitted to use other algorithms, so long as they adhere to the
  49  * general contracts for all the methods.
  50  * <p>
  51  * The algorithms implemented by class {@code Random} use a
  52  * {@code protected} utility method that on each invocation can supply


 497      *         standard deviation {@code 1.0} from this random number
 498      *         generator's sequence
 499      */
 500     synchronized public double nextGaussian() {
 501         // See Knuth, ACP, Section 3.4.1 Algorithm C.
 502         if (haveNextNextGaussian) {
 503             haveNextNextGaussian = false;
 504             return nextNextGaussian;
 505         } else {
 506             double v1, v2, s;
 507             do {
 508                 v1 = 2 * nextDouble() - 1; // between -1 and 1
 509                 v2 = 2 * nextDouble() - 1; // between -1 and 1
 510                 s = v1 * v1 + v2 * v2;
 511             } while (s >= 1 || s == 0);
 512             double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s)/s);
 513             nextNextGaussian = v2 * multiplier;
 514             haveNextNextGaussian = true;
 515             return v1 * multiplier;
 516         }
 517     }
 518 
 519     /**
 520      * Returns a stream of pseudorandom, uniformly distributed
 521      * {@code integer} values from this random number generator's
 522      * sequence. Values are obtained as needed by calling
 523      * {@link #nextInt()}.
 524      *
 525      * @return an infinite stream of {@code integer} values
 526      * @since 1.8
 527      */
 528     public IntStream ints() {
 529         return IntStream.generate(this::nextInt);
 530     }
 531 
 532     /**
 533      * Returns a stream of pseudorandom, uniformly distributed
 534      * {@code long} values from this random number generator's
 535      * sequence. Values are obtained as needed by calling
 536      * {@link #nextLong()}.
 537      *
 538      * @return an infinite stream of {@code long} values
 539      * @since 1.8
 540      */
 541     public LongStream longs() {
 542         return LongStream.generate(this::nextLong);
 543     }
 544 
 545     /**
 546      * Returns a stream of pseudorandom, uniformly distributed
 547      * {@code double} values between {@code 0.0} and {@code 1.0}
 548      * from this random number generator's sequence. Values are
 549      * obtained as needed by calling {@link #nextDouble()}.
 550      *
 551      * @return an infinite stream of {@code double} values
 552      * @since 1.8
 553      */
 554     public DoubleStream doubles() {
 555         return DoubleStream.generate(this::nextDouble);
 556     }
 557 
 558     /**
 559      * Returns a stream of pseudorandom, Gaussian ("normally")
 560      * distributed {@code double} values with mean {@code 0.0}
 561      * and standard deviation {@code 1.0} from this random number
 562      * generator's sequence. Values are obtained as needed by
 563      * calling {@link #nextGaussian()}.
 564      *
 565      * @return an infinite stream of {@code double} values
 566      * @since 1.8
 567      */
 568     public DoubleStream gaussians() {
 569         return DoubleStream.generate(this::nextGaussian);
 570     }
 571 
 572     /**
 573      * Serializable fields for Random.
 574      *
 575      * @serialField    seed long
 576      *              seed for random computations
 577      * @serialField    nextNextGaussian double
 578      *              next Gaussian to be returned
 579      * @serialField      haveNextNextGaussian boolean
 580      *              nextNextGaussian is valid
 581      */
 582     private static final ObjectStreamField[] serialPersistentFields = {
 583         new ObjectStreamField("seed", Long.TYPE),
 584         new ObjectStreamField("nextNextGaussian", Double.TYPE),
 585         new ObjectStreamField("haveNextNextGaussian", Boolean.TYPE)
 586     };
 587 
 588     /**
 589      * Reconstitute the {@code Random} instance from a stream (that is,