1 |
|
/* |
2 |
< |
* %W% %E% |
2 |
> |
* Copyright 1995-2007 Sun Microsystems, Inc. All Rights Reserved. |
3 |
> |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
|
* |
5 |
< |
* Copyright 2007 Sun Microsystems, Inc. All rights reserved. |
6 |
< |
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. |
5 |
> |
* This code is free software; you can redistribute it and/or modify it |
6 |
> |
* under the terms of the GNU General Public License version 2 only, as |
7 |
> |
* published by the Free Software Foundation. Sun designates this |
8 |
> |
* particular file as subject to the "Classpath" exception as provided |
9 |
> |
* by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
22 |
> |
* CA 95054 USA or visit www.sun.com if you need additional information or |
23 |
> |
* have any questions. |
24 |
|
*/ |
25 |
|
|
26 |
|
package java.util; |
118 |
|
synchronized public void setSeed(long seed) { |
119 |
|
seed = (seed ^ multiplier) & mask; |
120 |
|
this.seed.set(seed); |
121 |
< |
haveNextNextGaussian = false; |
121 |
> |
haveNextNextGaussian = false; |
122 |
|
} |
123 |
|
|
124 |
|
/** |
150 |
|
long oldseed, nextseed; |
151 |
|
AtomicLong seed = this.seed; |
152 |
|
do { |
153 |
< |
oldseed = seed.get(); |
154 |
< |
nextseed = (oldseed * multiplier + addend) & mask; |
153 |
> |
oldseed = seed.get(); |
154 |
> |
nextseed = (oldseed * multiplier + addend) & mask; |
155 |
|
} while (!seed.compareAndSet(oldseed, nextseed)); |
156 |
|
return (int)(nextseed >>> (48 - bits)); |
157 |
|
} |
176 |
|
* @since 1.1 |
177 |
|
*/ |
178 |
|
public void nextBytes(byte[] bytes) { |
179 |
< |
for (int i = 0, len = bytes.length; i < len; ) |
180 |
< |
for (int rnd = nextInt(), |
181 |
< |
n = Math.min(len - i, Integer.SIZE/Byte.SIZE); |
182 |
< |
n-- > 0; rnd >>= Byte.SIZE) |
183 |
< |
bytes[i++] = (byte)rnd; |
179 |
> |
for (int i = 0, len = bytes.length; i < len; ) |
180 |
> |
for (int rnd = nextInt(), |
181 |
> |
n = Math.min(len - i, Integer.SIZE/Byte.SIZE); |
182 |
> |
n-- > 0; rnd >>= Byte.SIZE) |
183 |
> |
bytes[i++] = (byte)rnd; |
184 |
|
} |
185 |
|
|
186 |
|
/** |
202 |
|
* value from this random number generator's sequence |
203 |
|
*/ |
204 |
|
public int nextInt() { |
205 |
< |
return next(32); |
205 |
> |
return next(32); |
206 |
|
} |
207 |
|
|
208 |
|
/** |
253 |
|
* successive calls to this method if n is a small power of two. |
254 |
|
* |
255 |
|
* @param n the bound on the random number to be returned. Must be |
256 |
< |
* positive. |
256 |
> |
* positive. |
257 |
|
* @return the next pseudorandom, uniformly distributed {@code int} |
258 |
|
* value between {@code 0} (inclusive) and {@code n} (exclusive) |
259 |
|
* from this random number generator's sequence |
317 |
|
* |
318 |
|
* @return the next pseudorandom, uniformly distributed |
319 |
|
* {@code boolean} value from this random number generator's |
320 |
< |
* sequence |
320 |
> |
* sequence |
321 |
|
* @since 1.2 |
322 |
|
*/ |
323 |
|
public boolean nextBoolean() { |
324 |
< |
return next(1) != 0; |
324 |
> |
return next(1) != 0; |
325 |
|
} |
326 |
|
|
327 |
|
/** |
408 |
|
*/ |
409 |
|
public double nextDouble() { |
410 |
|
return (((long)(next(26)) << 27) + next(27)) |
411 |
< |
/ (double)(1L << 53); |
411 |
> |
/ (double)(1L << 53); |
412 |
|
} |
413 |
|
|
414 |
|
private double nextNextGaussian; |
462 |
|
synchronized public double nextGaussian() { |
463 |
|
// See Knuth, ACP, Section 3.4.1 Algorithm C. |
464 |
|
if (haveNextNextGaussian) { |
465 |
< |
haveNextNextGaussian = false; |
466 |
< |
return nextNextGaussian; |
467 |
< |
} else { |
465 |
> |
haveNextNextGaussian = false; |
466 |
> |
return nextNextGaussian; |
467 |
> |
} else { |
468 |
|
double v1, v2, s; |
469 |
< |
do { |
469 |
> |
do { |
470 |
|
v1 = 2 * nextDouble() - 1; // between -1 and 1 |
471 |
< |
v2 = 2 * nextDouble() - 1; // between -1 and 1 |
471 |
> |
v2 = 2 * nextDouble() - 1; // between -1 and 1 |
472 |
|
s = v1 * v1 + v2 * v2; |
473 |
< |
} while (s >= 1 || s == 0); |
474 |
< |
double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s)/s); |
475 |
< |
nextNextGaussian = v2 * multiplier; |
476 |
< |
haveNextNextGaussian = true; |
477 |
< |
return v1 * multiplier; |
473 |
> |
} while (s >= 1 || s == 0); |
474 |
> |
double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s)/s); |
475 |
> |
nextNextGaussian = v2 * multiplier; |
476 |
> |
haveNextNextGaussian = true; |
477 |
> |
return v1 * multiplier; |
478 |
|
} |
479 |
|
} |
480 |
|
|
503 |
|
|
504 |
|
ObjectInputStream.GetField fields = s.readFields(); |
505 |
|
|
506 |
< |
// The seed is read in as {@code long} for |
507 |
< |
// historical reasons, but it is converted to an AtomicLong. |
508 |
< |
long seedVal = (long) fields.get("seed", -1L); |
506 |
> |
// The seed is read in as {@code long} for |
507 |
> |
// historical reasons, but it is converted to an AtomicLong. |
508 |
> |
long seedVal = (long) fields.get("seed", -1L); |
509 |
|
if (seedVal < 0) |
510 |
|
throw new java.io.StreamCorruptedException( |
511 |
|
"Random: invalid seed"); |
518 |
|
* Save the {@code Random} instance to a stream. |
519 |
|
*/ |
520 |
|
synchronized private void writeObject(ObjectOutputStream s) |
521 |
< |
throws IOException { |
521 |
> |
throws IOException { |
522 |
|
|
523 |
|
// set the values of the Serializable fields |
524 |
|
ObjectOutputStream.PutField fields = s.putFields(); |
525 |
|
|
526 |
< |
// The seed is serialized as a long for historical reasons. |
526 |
> |
// The seed is serialized as a long for historical reasons. |
527 |
|
fields.put("seed", seed.get()); |
528 |
|
fields.put("nextNextGaussian", nextNextGaussian); |
529 |
|
fields.put("haveNextNextGaussian", haveNextNextGaussian); |
539 |
|
try { |
540 |
|
seedOffset = unsafe.objectFieldOffset |
541 |
|
(Random.class.getDeclaredField("seed")); |
542 |
< |
} catch (Exception ex) { throw new Error(ex); } |
542 |
> |
} catch (Exception ex) { throw new Error(ex); } |
543 |
|
} |
544 |
|
private void resetSeed(long seedVal) { |
545 |
|
unsafe.putObjectVolatile(this, seedOffset, new AtomicLong(seedVal)); |