1 /*
   2  * Copyright (c) 2015, 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 import java.util.Random;
  25 import java.util.SplittableRandom;
  26 
  27 /**
  28  * Factory class which generates and prints to STDOUT a long-valued seed
  29  * for use in initializing a PRNG.  An instance of {@code Random} or
  30  * {@code SplittableRandom} may likewise be obtained.
  31  */
  32 public class RandomFactory {
  33     /**
  34      * Attempt to obtain the seed from the value of the "seed" property.
  35      * @return The seed or {@code null} if the "seed" property was not set or
  36      *         could not be parsed.
  37      */
  38     private static Long getSystemSeed() {
  39         Long seed = null;
  40         try {
  41             // note that Long.valueOf(null) also throws a
  42             // NumberFormatException so if the property is undefined this
  43             // will still work correctly
  44             seed = Long.valueOf(System.getProperty("seed"));
  45         } catch (NumberFormatException e) {
  46             // do nothing: seed is still null
  47         }
  48 
  49         return seed;
  50     }
  51 
  52     /**
  53      * Obtain a seed from an independent PRNG.
  54      *
  55      * @return A random seed.
  56      */
  57     private static long getRandomSeed() {
  58         return new Random().nextLong();
  59     }
  60 
  61     /**
  62      * Obtain and print to STDOUT a seed appropriate for initializing a PRNG.
  63      * If the system property "seed" is set and has value which may be correctly
  64      * parsed it is used, otherwise a seed is generated using an independent
  65      * PRNG.
  66      *
  67      * @return The seed.
  68      */
  69     public static long getSeed() {
  70         Long seed = getSystemSeed();
  71         if (seed == null) {
  72             seed = getRandomSeed();
  73         }
  74         System.out.println("Seed from RandomFactory = "+seed+"L");
  75         return seed;
  76     }
  77 
  78     /**
  79      * Obtain and print to STDOUT a seed and use it to initialize a new
  80      * {@code Random} instance which is returned. If the system
  81      * property "seed" is set and has value which may be correctly parsed it
  82      * is used, otherwise a seed is generated using an independent PRNG.
  83      *
  84      * @return The {@code Random} instance.
  85      */
  86     public static Random getRandom() {
  87         return new Random(getSeed());
  88     }
  89 
  90     /**
  91      * Obtain and print to STDOUT a seed and use it to initialize a new
  92      * {@code SplittableRandom} instance which is returned. If the system
  93      * property "seed" is set and has value which may be correctly parsed it
  94      * is used, otherwise a seed is generated using an independent PRNG.
  95      *
  96      * @return The {@code SplittableRandom} instance.
  97      */
  98     public static SplittableRandom getSplittableRandom() {
  99         return new SplittableRandom(getSeed());
 100     }
 101 }