src/share/classes/java/util/SplittableRandom.java

Print this page




   8  * particular file as subject to the "Classpath" exception as provided
   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 
  28 import java.net.NetworkInterface;
  29 import java.util.concurrent.atomic.AtomicLong;
  30 import java.util.function.IntConsumer;
  31 import java.util.function.LongConsumer;
  32 import java.util.function.DoubleConsumer;
  33 import java.util.stream.StreamSupport;
  34 import java.util.stream.IntStream;
  35 import java.util.stream.LongStream;
  36 import java.util.stream.DoubleStream;
  37 
  38 /**
  39  * A generator of uniform pseudorandom values applicable for use in
  40  * (among other contexts) isolated parallel computations that may
  41  * generate subtasks. Class {@code SplittableRandom} supports methods for
  42  * producing pseudorandom numbers of type {@code int}, {@code long},
  43  * and {@code double} with similar usages as for class
  44  * {@link java.util.Random} but differs in the following ways:
  45  *
  46  * <ul>
  47  *
  48  * <li>Series of generated values pass the DieHarder suite testing


 210         z = (z ^ (z >>> 33)) * 0xff51afd7ed558ccdL; // MurmurHash3 mix constants
 211         z = (z ^ (z >>> 33)) * 0xc4ceb9fe1a85ec53L;
 212         z = (z ^ (z >>> 33)) | 1L;                  // force to be odd
 213         int n = Long.bitCount(z ^ (z >>> 1));       // ensure enough transitions
 214         return (n < 24) ? z ^ 0xaaaaaaaaaaaaaaaaL : z;
 215     }
 216 
 217     /**
 218      * Adds gamma to seed.
 219      */
 220     private long nextSeed() {
 221         return seed += gamma;
 222     }
 223 
 224     /**
 225      * The seed generator for default constructors.
 226      */
 227     private static final AtomicLong defaultGen = new AtomicLong(initialSeed());
 228 
 229     private static long initialSeed() {
 230         String pp = java.security.AccessController.doPrivileged(
 231                 new sun.security.action.GetPropertyAction(
 232                         "java.util.secureRandomSeed"));
 233         if (pp != null && pp.equalsIgnoreCase("true")) {
 234             byte[] seedBytes = java.security.SecureRandom.getSeed(8);
 235             long s = (long)(seedBytes[0]) & 0xffL;
 236             for (int i = 1; i < 8; ++i)
 237                 s = (s << 8) | ((long)(seedBytes[i]) & 0xffL);
 238             return s;

 239         }
 240         long h = 0L;
 241         try {
 242             Enumeration<NetworkInterface> ifcs =
 243                     NetworkInterface.getNetworkInterfaces();
 244             boolean retry = false; // retry once if getHardwareAddress is null
 245             while (ifcs.hasMoreElements()) {
 246                 NetworkInterface ifc = ifcs.nextElement();
 247                 if (!ifc.isVirtual()) { // skip fake addresses
 248                     byte[] bs = ifc.getHardwareAddress();
 249                     if (bs != null) {
 250                         int n = bs.length;
 251                         int m = Math.min(n >>> 1, 4);
 252                         for (int i = 0; i < m; ++i)
 253                             h = (h << 16) ^ (bs[i] << 8) ^ bs[n-1-i];
 254                         if (m < 4)
 255                             h = (h << 8) ^ bs[n-1-m];
 256                         h = mix64(h);
 257                         break;
 258                     }
 259                     else if (!retry)
 260                         retry = true;
 261                     else
 262                         break;
 263                 }
 264             }
 265         } catch (Exception ignore) {
 266         }
 267         return (h ^ mix64(System.currentTimeMillis()) ^
 268                 mix64(System.nanoTime()));
 269     }
 270 
 271     // IllegalArgumentException messages
 272     static final String BadBound = "bound must be positive";
 273     static final String BadRange = "bound must be greater than origin";
 274     static final String BadSize  = "size must be non-negative";
 275 
 276     /*
 277      * Internal versions of nextX methods used by streams, as well as
 278      * the public nextX(origin, bound) methods.  These exist mainly to
 279      * avoid the need for multiple versions of stream spliterators
 280      * across the different exported forms of streams.
 281      */
 282 
 283     /**
 284      * The form of nextLong used by LongStream Spliterators.  If
 285      * origin is greater than bound, acts as unbounded form of
 286      * nextLong, else as bounded form.
 287      *
 288      * @param origin the least value, unless greater than bound




   8  * particular file as subject to the "Classpath" exception as provided
   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 
  28 import sun.security.provider.SeedGenerator;
  29 import java.util.concurrent.atomic.AtomicLong;
  30 import java.util.function.IntConsumer;
  31 import java.util.function.LongConsumer;
  32 import java.util.function.DoubleConsumer;
  33 import java.util.stream.StreamSupport;
  34 import java.util.stream.IntStream;
  35 import java.util.stream.LongStream;
  36 import java.util.stream.DoubleStream;
  37 
  38 /**
  39  * A generator of uniform pseudorandom values applicable for use in
  40  * (among other contexts) isolated parallel computations that may
  41  * generate subtasks. Class {@code SplittableRandom} supports methods for
  42  * producing pseudorandom numbers of type {@code int}, {@code long},
  43  * and {@code double} with similar usages as for class
  44  * {@link java.util.Random} but differs in the following ways:
  45  *
  46  * <ul>
  47  *
  48  * <li>Series of generated values pass the DieHarder suite testing


 210         z = (z ^ (z >>> 33)) * 0xff51afd7ed558ccdL; // MurmurHash3 mix constants
 211         z = (z ^ (z >>> 33)) * 0xc4ceb9fe1a85ec53L;
 212         z = (z ^ (z >>> 33)) | 1L;                  // force to be odd
 213         int n = Long.bitCount(z ^ (z >>> 1));       // ensure enough transitions
 214         return (n < 24) ? z ^ 0xaaaaaaaaaaaaaaaaL : z;
 215     }
 216 
 217     /**
 218      * Adds gamma to seed.
 219      */
 220     private long nextSeed() {
 221         return seed += gamma;
 222     }
 223 
 224     /**
 225      * The seed generator for default constructors.
 226      */
 227     private static final AtomicLong defaultGen = new AtomicLong(initialSeed());
 228 
 229     private static long initialSeed() {
 230         try (SeedGenerator sg = SeedGenerator.getNativeInstance()) {
 231             byte[] seedBytes = new byte[8];
 232             sg.getSeedBytes(seedBytes);


 233             long s = (long)(seedBytes[0]) & 0xffL;
 234             for (int i = 1; i < 8; ++i)
 235                 s = (s << 8) | ((long)(seedBytes[i]) & 0xffL);
 236             return s ^ mix64(System.currentTimeMillis()) ^
 237                    mix64(System.nanoTime());
 238         }





























 239     }
 240 
 241     // IllegalArgumentException messages
 242     static final String BadBound = "bound must be positive";
 243     static final String BadRange = "bound must be greater than origin";
 244     static final String BadSize  = "size must be non-negative";
 245 
 246     /*
 247      * Internal versions of nextX methods used by streams, as well as
 248      * the public nextX(origin, bound) methods.  These exist mainly to
 249      * avoid the need for multiple versions of stream spliterators
 250      * across the different exported forms of streams.
 251      */
 252 
 253     /**
 254      * The form of nextLong used by LongStream Spliterators.  If
 255      * origin is greater than bound, acts as unbounded form of
 256      * nextLong, else as bounded form.
 257      *
 258      * @param origin the least value, unless greater than bound