com.ibm.security12.java.util
Class Random12
java.lang.Object
|
+--java.util.Random
|
+--com.ibm.security12.java.util.Random12
- public class Random12
- extends Random
Random12 is an improved version of java.util.Random with the added
functionality of two new methods: nextInt() and nextBoolean().
JDK 1.2 introduces a two new methods to java.util.Random.
The nextInt() method returns a pseudorandom integer between zero
and the supplied method argument. The nextBoolean() method returns
a pseudorandom boolean value and, when used repeatedly, should furnish
an even distribution of true and false values. The
functionality was required by other Migration Aid classes but developers
could also use Random12
methods in applications.
The following example illustrates the two Random12 methods.
. . .
Random12 r12 = new Random12();
int i;
int index;
int collisions = 0;
int falseVals = 0;
int trueVals = 0;
int[] arr = new int[1000];
// Set array entries to -1
for (i = 0; i < 1000; i++)
arr[i] = -1;
for (i = 0, i < 1000; i++) {
index = r12.nextInt(1000);
if (arr[index] == -1)
arr[index] = 0;
else collisions++;
if (r12.nextBoolean())
trueVals++;
else falseVals++;
}
System.out.println("Number of random integer collisions: "+collisions);
System.out.println("Number of random true values: "+trueVals);
System.out.println("Number of random false values: "+falseVals);
. . .
- Version:
- 1.1, 98/06/01
- Author:
- D. Kent Soper
- See Also:
- Serialized Form
Constructor Summary |
Random12()
Creates a Random12 random number generator instance. |
Method Summary |
boolean |
nextBoolean()
Returns the next pseudorandom, uniformly distributed
boolean value from this random number generator's
sequence. |
int |
nextInt(int n)
Returns a pseudorandom, uniformly distributed int value
between 0 (inclusive) and the specified value (exclusive), drawn from
this random number generator's sequence. |
Methods inherited from class java.lang.Object |
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
toString,
wait,
wait,
wait |
Random12
public Random12()
- Creates a
Random12
random number generator instance.
Its seed is based on the current time.
- See Also:
Random.Random()
nextInt
public int nextInt(int n)
- Returns a pseudorandom, uniformly distributed int value
between 0 (inclusive) and the specified value (exclusive), drawn from
this random number generator's sequence. The general contract of
nextInt is that one int value in the specified range
is pseudorandomly generated and returned. All n possible
int values are produced with (approximately) equal
probability. The method nextInt(int n) is implemented by
class Random as follows:
public int nextInt(int n) {
if (n<=0)
throw new IllegalArgumentException("n must be positive");
int bits, val;
do {
bits = next(31);
val = bits % n;
} while(bits - val + (n-1) < 0);
return val;
}
The hedge "approximately" is used in the foregoing description only
because the next method is only approximately an unbiased source of
independently chosen bits. If it were a perfect source or randomly
chosen bits, then the algorithm shown would choose int
values from the stated range with perfect uniformity.
The algorithm is slightly tricky. It rejects values that would result
in an uneven distribution (due to the fact that 2^31 is not divisible
by n). The probability of a value being rejected depends on n. If n is
a power of two, the probability of rejection is zero. The worst case
is n=2^30+1, for which the probability of a reject is 1/2, and the
expected number of iterations before the loop terminates is 2.
- Returns:
- a pseudorandom, uniformly distributed int
value between 0 (inclusive) and n (exclusive).
- Throws:
- IllegalArgumentException - n is not positive.
- Overrides:
- nextInt in class Random
- Since:
- JDK1.2
nextBoolean
public boolean nextBoolean()
- Returns the next pseudorandom, uniformly distributed
boolean
value from this random number generator's
sequence. The general contract of nextBoolean is that one
boolean value is pseudorandomly generated and returned. The
values true
and false
are produced with
(approximately) equal probability. The method nextBoolean is
implemented by class Random as follows:
public boolean nextBoolean() {return next(1) != 0;}
- Returns:
- the next pseudorandom, uniformly distributed
boolean
value from this random number generator's
sequence. - Overrides:
- nextBoolean in class Random
- Since:
- JDK1.2