1 |
|
/* |
2 |
< |
* @(#)Random.java 1.39 03/01/23 |
2 |
> |
* %W% %E% |
3 |
|
* |
4 |
|
* Copyright 2003 Sun Microsystems, Inc. All rights reserved. |
5 |
|
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. |
33 |
|
* class <code>Math</code> simpler to use. |
34 |
|
* |
35 |
|
* @author Frank Yellin |
36 |
< |
* @version 1.39, 01/23/03 |
36 |
> |
* @version %I%, %G% |
37 |
|
* @see java.lang.Math#random() |
38 |
|
* @since JDK1.0 |
39 |
|
*/ |
55 |
|
private final static long addend = 0xBL; |
56 |
|
private final static long mask = (1L << 48) - 1; |
57 |
|
|
58 |
< |
/** |
59 |
< |
* Creates a new random number generator. Its seed is initialized to |
60 |
< |
* a value based on the current time: |
61 |
< |
* <blockquote><pre> |
62 |
< |
* public Random() { this(System.currentTimeMillis()); }</pre></blockquote> |
63 |
< |
* Two Random objects created within the same millisecond will have |
64 |
< |
* the same sequence of random numbers. |
65 |
< |
* |
66 |
< |
* @see java.lang.System#currentTimeMillis() |
58 |
> |
/** |
59 |
> |
* Creates a new random number generator. This constructor sets |
60 |
> |
* the seed of the random number generator to a value very likely |
61 |
> |
* to be distinct from any other invocation of this constructor. |
62 |
|
*/ |
63 |
< |
public Random() { this(System.currentTimeMillis()); } |
63 |
> |
public Random() { this(++seedUniquifier + System.nanoTime()); } |
64 |
> |
private static volatile long seedUniquifier = 8682522807148012L; |
65 |
|
|
66 |
|
/** |
67 |
|
* Creates a new random number generator using a single |
134 |
|
*/ |
135 |
|
protected int next(int bits) { |
136 |
|
long oldseed, nextseed; |
137 |
+ |
AtomicLong seed = this.seed; |
138 |
|
do { |
139 |
< |
oldseed = seed.get(); |
140 |
< |
nextseed = (oldseed * multiplier + addend) & mask; |
139 |
> |
oldseed = seed.get(); |
140 |
> |
nextseed = (oldseed * multiplier + addend) & mask; |
141 |
|
} while (!seed.compareAndSet(oldseed, nextseed)); |
142 |
|
return (int)(nextseed >>> (48 - bits)); |
143 |
|
} |