1 /*
   2  * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package jdk.test.lib;
  25 
  26 import java.util.Random;
  27 import java.util.SplittableRandom;
  28 
  29 /**
  30  * Factory class which generates and prints to STDOUT a long-valued seed
  31  * for use in initializing a PRNG.  An instance of {@code Random} or
  32  * {@code SplittableRandom} may likewise be obtained.
  33  */
  34 public class RandomFactory {
  35     /**
  36      * Attempt to obtain the seed from the value of the "seed" property.
  37      * @return The seed or {@code null} if the "seed" property was not set or
  38      *         could not be parsed.
  39      */
  40     private static Long getSystemSeed() {
  41         Long seed = null;
  42         try {
  43             // note that Long.valueOf(null) also throws a
  44             // NumberFormatException so if the property is undefined this
  45             // will still work correctly
  46             seed = Long.valueOf(System.getProperty("seed"));
  47         } catch (NumberFormatException e) {
  48             // do nothing: seed is still null
  49         }
  50 
  51         return seed;
  52     }
  53 
  54     /**
  55      * Obtain a seed from an independent PRNG.
  56      *
  57      * @return A random seed.
  58      */
  59     private static long getRandomSeed() {
  60         return new Random().nextLong();
  61     }
  62 
  63     /**
  64      * Obtain and print to STDOUT a seed appropriate for initializing a PRNG.
  65      * If the system property "seed" is set and has value which may be correctly
  66      * parsed it is used, otherwise a seed is generated using an independent
  67      * PRNG.
  68      *
  69      * @return The seed.
  70      */
  71     public static long getSeed() {
  72         Long seed = getSystemSeed();
  73         if (seed == null) {
  74             seed = getRandomSeed();
  75         }
  76         System.out.println("Seed from RandomFactory = "+seed+"L");
  77         return seed;
  78     }
  79 
  80     /**
  81      * Obtain and print to STDOUT a seed and use it to initialize a new
  82      * {@code Random} instance which is returned. If the system
  83      * property "seed" is set and has value which may be correctly parsed it
  84      * is used, otherwise a seed is generated using an independent PRNG.
  85      *
  86      * @return The {@code Random} instance.
  87      */
  88     public static Random getRandom() {
  89         return new Random(getSeed());
  90     }
  91 
  92     /**
  93      * Obtain and print to STDOUT a seed and use it to initialize a new
  94      * {@code SplittableRandom} instance which is returned. If the system
  95      * property "seed" is set and has value which may be correctly parsed it
  96      * is used, otherwise a seed is generated using an independent PRNG.
  97      *
  98      * @return The {@code SplittableRandom} instance.
  99      */
 100     public static SplittableRandom getSplittableRandom() {
 101         return new SplittableRandom(getSeed());
 102     }
 103 }