jdk/test/java/util/Random/DistinctSeeds.java
author smarks
Mon, 20 Dec 2010 13:47:04 -0800
changeset 7803 56bc97d69d93
parent 5506 202f599c92aa
child 9242 ef138d47df58
permissions -rw-r--r--
6880112: Project Coin: Port JDK core library code to use diamond operator Reviewed-by: darcy, lancea, alanb, briangoetz, mduigou, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5468
diff changeset
    18
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5468
diff changeset
    19
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5468
diff changeset
    20
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 * This file is available under and governed by the GNU General Public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * License version 2 only, as published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * However, the following notice accompanied the original version of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * file:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * Written by Doug Lea with assistance from members of JCP JSR-166
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * Expert Group and released to the public domain, as explained at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * http://creativecommons.org/licenses/publicdomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @test
5468
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 2
diff changeset
    36
 * @bug 4949279 6937857
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @summary Independent instantiations of Random() have distinct seeds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
5468
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 2
diff changeset
    40
import java.util.ArrayList;
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 2
diff changeset
    41
import java.util.HashSet;
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 2
diff changeset
    42
import java.util.List;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.Random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public class DistinctSeeds {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        // Strictly speaking, it is possible for these to randomly fail,
5468
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 2
diff changeset
    48
        // but the probability should be small (approximately 2**-48).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        if (new Random().nextLong() == new Random().nextLong() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            new Random().nextLong() == new Random().nextLong())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            throw new RuntimeException("Random() seeds not unique.");
5468
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 2
diff changeset
    52
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 2
diff changeset
    53
        // Now try generating seeds concurrently
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 2
diff changeset
    54
        class RandomCollector implements Runnable {
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 2
diff changeset
    55
            long[] randoms = new long[1<<17];
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 2
diff changeset
    56
            public void run() {
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 2
diff changeset
    57
                for (int i = 0; i < randoms.length; i++)
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 2
diff changeset
    58
                    randoms[i] = new Random().nextLong();
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 2
diff changeset
    59
            }
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 2
diff changeset
    60
        }
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 2
diff changeset
    61
        final int threadCount = 2;
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
    62
        List<RandomCollector> collectors = new ArrayList<>();
5468
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 2
diff changeset
    63
        List<Thread> threads = new ArrayList<Thread>();
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 2
diff changeset
    64
        for (int i = 0; i < threadCount; i++) {
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 2
diff changeset
    65
            RandomCollector r = new RandomCollector();
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 2
diff changeset
    66
            collectors.add(r);
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 2
diff changeset
    67
            threads.add(new Thread(r));
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 2
diff changeset
    68
        }
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 2
diff changeset
    69
        for (Thread thread : threads)
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 2
diff changeset
    70
            thread.start();
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 2
diff changeset
    71
        for (Thread thread : threads)
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 2
diff changeset
    72
            thread.join();
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 2
diff changeset
    73
        int collisions = 0;
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 2
diff changeset
    74
        HashSet<Long> s = new HashSet<Long>();
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 2
diff changeset
    75
        for (RandomCollector r : collectors) {
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 2
diff changeset
    76
            for (long x : r.randoms) {
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 2
diff changeset
    77
                if (s.contains(x))
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 2
diff changeset
    78
                    collisions++;
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 2
diff changeset
    79
                s.add(x);
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 2
diff changeset
    80
            }
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 2
diff changeset
    81
        }
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 2
diff changeset
    82
        System.out.printf("collisions=%d%n", collisions);
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 2
diff changeset
    83
        if (collisions > 10)
c9aa7dfb4f78 6937857: Concurrent calls to new Random() not random enough
martin
parents: 2
diff changeset
    84
            throw new Error("too many collisions");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
}