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


 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()));




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


 219         int n = Long.bitCount(z ^ (z >>> 1));       // ensure enough transitions
 220         return (n < 24) ? z ^ 0xaaaaaaaaaaaaaaaaL : z;
 221     }
 222 
 223     /**
 224      * Adds gamma to seed.
 225      */
 226     private long nextSeed() {
 227         return seed += gamma;
 228     }
 229 
 230     /**
 231      * The seed generator for default constructors.
 232      */
 233     private static final AtomicLong defaultGen = new AtomicLong(initialSeed());
 234 
 235     private static long initialSeed() {
 236         String pp = java.security.AccessController.doPrivileged(
 237                 new sun.security.action.GetPropertyAction(
 238                         "java.util.secureRandomSeed"));
 239         if (pp != null) {
 240             SecureRandom sr;
 241             if (pp.equalsIgnoreCase("true")) {
 242                 sr = new SecureRandom();
 243             } else {
 244                 String[] args = pp.split(",", 2);
 245                 try {
 246                     sr = args.length > 1
 247                          ? SecureRandom.getInstance(args[0], args[1])
 248                          : SecureRandom.getInstance(args[0]);
 249                 } catch (NoSuchAlgorithmException | NoSuchProviderException e) {
 250                     throw new IllegalArgumentException(
 251                         "Invalid java.util.secureRandomSeed property: " + pp, e);
 252                 }
 253             }
 254             byte[] seedBytes = sr.generateSeed(8);
 255             long s = (long)(seedBytes[0]) & 0xffL;
 256             for (int i = 1; i < 8; ++i)
 257                 s = (s << 8) | ((long)(seedBytes[i]) & 0xffL);
 258             return s;
 259         }
 260         long h = 0L;
 261         try {
 262             Enumeration<NetworkInterface> ifcs =
 263                     NetworkInterface.getNetworkInterfaces();
 264             JavaNetAccess jna = SharedSecrets.getJavaNetAccess();
 265             boolean retry = false; // retry once if getHardwareAddress is null
 266             while (ifcs.hasMoreElements()) {
 267                 NetworkInterface ifc = ifcs.nextElement();
 268                 if (!ifc.isVirtual()) { // skip fake addresses
 269                     byte[] bs = jna.getHardwareAddress0(ifc);
 270                     if (bs != null) {
 271                         int n = bs.length;
 272                         int m = Math.min(n >>> 1, 4);
 273                         for (int i = 0; i < m; ++i)
 274                             h = (h << 16) ^ (bs[i] << 8) ^ bs[n-1-i];
 275                         if (m < 4)
 276                             h = (h << 8) ^ bs[n-1-m];
 277                         h = mix64(h);
 278                         break;
 279                     }
 280                     else if (!retry)
 281                         retry = true;
 282                     else
 283                         break;
 284                 }
 285             }
 286         } catch (Exception ignore) {
 287         }
 288         return (h ^ mix64(System.currentTimeMillis()) ^
 289                 mix64(System.nanoTime()));