Random random = new Random(50);
for (int j = 0; j < 8; j++) {
System.out.print(" " + random.nextInt(100) + ", ");
}
System.out.println();
for (int j = 0; j < 8; j++) {
System.out.print(" " + random.nextInt(100) + ", ");
}
System.out.println();
Random random2 = new Random(50);
for (int j = 0; j < 8; j++) {
System.out.print(" " + random2.nextInt(100) + ", ");
}
System.out.println();
for (int j = 0; j < 8; j++) {
System.out.print(" " + random2.nextInt(100) + ", ");
}
System.out.println();
System.out.println("-----------------------------------");
Random random3 = new Random();
for (int j = 0; j < 8; j++) {
System.out.print(" " + random3.nextInt(100) + ", ");
}
System.out.println();
Random random4 = new Random();
for (int j = 0; j < 8; j++) {
System.out.print(" " + random4.nextInt(100) + ", ");
}
System.out.println();
17, 88, 93, 12, 51, 61, 36, 58,
16, 8, 0, 12, 0, 55, 28, 92,
17, 88, 93, 12, 51, 61, 36, 58,
16, 8, 0, 12, 0, 55, 28, 92,
-----------------------------------
5, 15, 5, 57, 89, 12, 54, 74,
95, 3, 95, 96, 23, 77, 57, 62,
带种子的随机数生成器,每次生成的随机数可能不同;但每一遍生成的随机数是相同的;
不带种子的随机数生成器每次生成的随机数可能不同;但每一遍生成的随机数也可能不同;
/**
* 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.
*/
public Random() {
this(seedUniquifier() ^ System.nanoTime());
}
/**
* Creates a new random number generator using a single {@code long} seed.
* The seed is the initial value of the internal state of the pseudorandom
* number generator which is maintained by method {@link #next}.
*
* The invocation {@code new Random(seed)} is equivalent to:
*
{@code
* Random rnd = new Random();
* rnd.setSeed(seed);}
*
* @param seed the initial seed
* @see #setSeed(long)
*/
public Random(long seed) {
if (getClass() == Random.class)
this.seed = new AtomicLong(initialScramble(seed));
else {
// subclass might have overriden setSeed
this.seed = new AtomicLong();
setSeed(seed);
}
}
private static long initialScramble(long seed) {
return (seed ^ multiplier) & mask;
}
/**
* Sets the seed of this random number generator using a single
* {@code long} seed. The general contract of {@code setSeed} is
* that it alters the state of this random number generator object
* so as to be in exactly the same state as if it had just been
* created with the argument {@code seed} as a seed. The method
* {@code setSeed} is implemented by class {@code Random} by
* atomically updating the seed to
* {@code (seed ^ 0x5DEECE66DL) & ((1L << 48) - 1)}
* and clearing the {@code haveNextNextGaussian} flag used by {@link
* #nextGaussian}.
*
* The implementation of {@code setSeed} by class {@code Random}
* happens to use only 48 bits of the given seed. In general, however,
* an overriding method may use all 64 bits of the {@code long}
* argument as a seed value.
*
* @param seed the initial seed
*/
synchronized public void setSeed(long seed) {
this.seed.set(initialScramble(seed));
haveNextNextGaussian = false;
}
以上基于java1.8
随机数是种子经过计算生成的。