Merge "Set random seeds post fork."
diff --git a/dalvik/src/main/java/dalvik/system/ZygoteHooks.java b/dalvik/src/main/java/dalvik/system/ZygoteHooks.java
index 11ea286..59d8820 100644
--- a/dalvik/src/main/java/dalvik/system/ZygoteHooks.java
+++ b/dalvik/src/main/java/dalvik/system/ZygoteHooks.java
@@ -29,7 +29,7 @@
 
     /**
      * Called by the zygote prior to every fork. Each call to {@code preFork}
-     * is followed by a matching call to {@link #postForkChild(int)} on the child
+     * is followed by a matching call to {@link #postForkChild(int, String)} on the child
      * process and {@link #postForkCommon()} on both the parent and the child
      * process. {@code postForkCommon} is called after {@code postForkChild} in
      * the child process.
@@ -47,6 +47,8 @@
      */
     public void postForkChild(int debugFlags, String instructionSet) {
         nativePostForkChild(token, debugFlags, instructionSet);
+
+        Math.setRandomSeedInternal(System.currentTimeMillis());
     }
 
     /**
diff --git a/luni/src/main/java/java/io/File.java b/luni/src/main/java/java/io/File.java
index d107c28..a07d34c 100644
--- a/luni/src/main/java/java/io/File.java
+++ b/luni/src/main/java/java/io/File.java
@@ -57,12 +57,6 @@
     private static final long serialVersionUID = 301077366599181567L;
 
     /**
-     * Reusing a Random makes temporary filenames slightly harder to predict.
-     * (Random is thread-safe.)
-     */
-    private static final Random tempFileRandom = new Random();
-
-    /**
      * The system-dependent character used to separate components in filenames ('/').
      * Use of this (rather than hard-coding '/') helps portability to other operating systems.
      *
@@ -1002,7 +996,7 @@
         }
         File result;
         do {
-            result = new File(tmpDirFile, prefix + tempFileRandom.nextInt() + suffix);
+            result = new File(tmpDirFile, prefix + Math.randomIntInternal() + suffix);
         } while (!result.createNewFile());
         return result;
     }
diff --git a/luni/src/main/java/java/lang/Math.java b/luni/src/main/java/java/lang/Math.java
index 86df784..a738511 100644
--- a/luni/src/main/java/java/lang/Math.java
+++ b/luni/src/main/java/java/lang/Math.java
@@ -35,7 +35,7 @@
      */
     public static final double PI = 3.141592653589793;
 
-    private static Random random;
+    private static final Random INSTANCE = new Random();
 
     /**
      * Prevents this class from being instantiated.
@@ -875,11 +875,25 @@
      *
      * @return a pseudo-random number.
      */
-    public static synchronized double random() {
-        if (random == null) {
-            random = new Random();
-        }
-        return random.nextDouble();
+    public static double random() {
+        return INSTANCE.nextDouble();
+    }
+
+    /**
+     * Set the seed for the pseudo random generator used by {@link #random()}
+     * and {@link #randomIntInternal()}.
+     *
+     * @hide for internal use only.
+     */
+    public static void setRandomSeedInternal(long seed) {
+        INSTANCE.setSeed(seed);
+    }
+
+    /**
+     * @hide for internal use only.
+     */
+    public static int randomIntInternal() {
+        return INSTANCE.nextInt();
     }
 
     /**
diff --git a/luni/src/main/java/java/util/concurrent/ConcurrentSkipListMap.java b/luni/src/main/java/java/util/concurrent/ConcurrentSkipListMap.java
index 803cd49..4a76104 100644
--- a/luni/src/main/java/java/util/concurrent/ConcurrentSkipListMap.java
+++ b/luni/src/main/java/java/util/concurrent/ConcurrentSkipListMap.java
@@ -293,11 +293,13 @@
 
     private static final long serialVersionUID = -8627078645895051609L;
 
-    /**
-     * Generates the initial random seed for the cheaper per-instance
-     * random number generators used in randomLevel.
-     */
-    private static final Random seedGenerator = new Random();
+//  BEGIN android-removed
+//  /**
+//   * Generates the initial random seed for the cheaper per-instance
+//   * random number generators used in randomLevel.
+//   */
+//  private static final Random seedGenerator = new Random();
+//  END android-removed
 
     /**
      * Special value used to identify base-level header
@@ -341,7 +343,13 @@
         entrySet = null;
         values = null;
         descendingMap = null;
-        randomSeed = seedGenerator.nextInt() | 0x0100; // ensure nonzero
+        // BEGIN android-changed
+        //
+        // Most processes are forked from the zygote, so they'll end up
+        // with the same random seed unless we take additional post fork
+        // measures.
+        randomSeed = Math.randomIntInternal() | 0x0100; // ensure nonzero
+        // END android-changed
         head = new HeadIndex<K,V>(new Node<K,V>(null, BASE_HEADER, null),
                                   null, null, 1);
     }
diff --git a/luni/src/main/java/libcore/io/IoUtils.java b/luni/src/main/java/libcore/io/IoUtils.java
index 5a19f17..737ceeb 100644
--- a/luni/src/main/java/libcore/io/IoUtils.java
+++ b/luni/src/main/java/libcore/io/IoUtils.java
@@ -30,8 +30,6 @@
 import static android.system.OsConstants.*;
 
 public final class IoUtils {
-    private static final Random TEMPORARY_DIRECTORY_PRNG = new Random();
-
     private IoUtils() {
     }
 
@@ -142,7 +140,7 @@
      */
     public static File createTemporaryDirectory(String prefix) {
         while (true) {
-            String candidateName = prefix + TEMPORARY_DIRECTORY_PRNG.nextInt();
+            String candidateName = prefix + Math.randomIntInternal();
             File result = new File(System.getProperty("java.io.tmpdir"), candidateName);
             if (result.mkdir()) {
                 return result;