Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6937857

Concurrent calls to new Random() not random enough

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 7
    • 7
    • core-libs
    • b94
    • generic
    • generic
    • Verified

      new Random() promises this:
          /**
           * Creates a new random number generator. This constructor sets
           * the seed of the random number generator to a value very likely
           * to be distinct from any other invocation of this constructor.
           */

      but if there are concurrent calls to new Random(), it does not
      do very well at fulfilling its contract.

      The following program should print out a number much closer to 0.

      import java.util.ArrayList;
      import java.util.HashSet;
      import java.util.List;
      import java.util.Random;

      public class RandomSeedCollisions {
          public static void main(String[] args) throws Throwable {
              class RandomCollector implements Runnable {
                  long[] randoms = new long[1<<16];
                  public void run() {
                      for (int i = 0; i < randoms.length; i++)
                          randoms[i] = new Random().nextLong();
                  }};
              final int threadCount = 2;
              List<RandomCollector> collectors = new ArrayList<RandomCollector>();
              List<Thread> threads = new ArrayList<Thread>();
              for (int i = 0; i < threadCount; i++) {
                  RandomCollector r = new RandomCollector();
                  collectors.add(r);
                  threads.add(new Thread(r));
              }
              for (Thread thread : threads)
                  thread.start();
              for (Thread thread : threads)
                  thread.join();
              int collisions = 0;
              HashSet<Long> s = new HashSet<Long>();
              for (RandomCollector r : collectors) {
                  for (long x : r.randoms) {
                      if (s.contains(x))
                          collisions++;
                      s.add(x);
                  }
              }
              System.out.printf("collisions=%d%n", collisions);
              if (collisions > 10)
                  throw new Error("too many collisions");
          }
      }

            martin Martin Buchholz
            sherman Xueming Shen
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: