|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectbb.util.RandomUtil
public final class RandomUtil
Provides static utility methods relating to Random
.
This class is multithread safe: all of its fields are multithread safe types.
Nested Class Summary | |
---|---|
static class |
RandomUtil.UnitTest
See the Overview page of the project's javadocs for a general description of this unit test class. |
Field Summary | |
---|---|
private static AtomicLong |
id
|
private static ThreadLocal<ec.util.MersenneTwisterFast> |
threadLocal
|
Constructor Summary | |
---|---|
private |
RandomUtil()
This private constructor suppresses the default (public) constructor, ensuring non-instantiability. |
Method Summary | |
---|---|
static ec.util.MersenneTwisterFast |
get()
Returns a MersenneTwisterFast instance that is local to the calling thread, so thread contention is guaranteed to never occur. |
static long |
makeSeed()
This method first attempts to generate a high quality seed by calling makeSeedSecure . |
static long |
makeSeedSecure()
Returns . |
static long |
makeSeedSecure(SecureRandom random)
Returns . |
static long |
makeSeedSecure(String algorithm)
Returns . |
static long |
makeSeedSecure(String algorithm,
String provider)
Returns . |
static long |
makeSeedUnique()
This seed value generating function attempts to satisfy these goals: return a unique result for each call of this method return a unique series of results for each different JVM invocation return results which are uniformly spread around the range of all possible long values It uses the following techniques: an internal serial id field is incremented upon each call, so each call is guaranteed a different value; this field determines the high order bits of the result each call uses the result of System.nanoTime
to determine the low order bits of the result;
this should be different each time the JVM is run (assuming that the system time is different)
a hash algorithm is applied to the above numbers before putting them into the high and low order parts of the result
Warnings:
the uniqueness goals cannot be guaranteed because the hash algorithm, while it is of high quality,
is not guaranteed to be a 1-1 function (i.e. 2 different input ints might get mapped to the same output int). |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private static final AtomicLong id
private static final ThreadLocal<ec.util.MersenneTwisterFast> threadLocal
Constructor Detail |
---|
private RandomUtil()
Method Detail |
---|
public static long makeSeed()
makeSeedSecure
.
This result should be of extremely high, cryptographic quality
(i.e. possibly come in part from a low level hardware source like diode noise, or at least from something like /dev/random,
as well as perhaps also satisfy some uniqueness aspects).
If any Exception is thrown by this step, it is caught and a value of 0 is assigned to the seed.
This method then generates an additional seed by calling makeSeedUnique
which is highly likely to satisfy some uniqueness requirements.
This second seed defends against potential problems in certain implementations of SecureRandom (or lack thereof) on some platforms.
A bitwise XOR of the two seeds is finally returned.
public static long makeSeedSecure() throws NoSuchAlgorithmException
makeSeedSecure
("SHA1PRNG")
.
NoSuchAlgorithmException
- if the SHA1PRNG algorithm is not available in the caller's environmentpublic static long makeSeedSecure(String algorithm) throws NoSuchAlgorithmException
makeSeedSecure
( SecureRandom.getInstance
(algorithm) )
.
NoSuchAlgorithmException
- if the requested algorithm is not available in the caller's environmentpublic static long makeSeedSecure(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderException, IllegalArgumentException
makeSeedSecure
( SecureRandom.getInstance
(algorithm, provider) )
.
NoSuchAlgorithmException
- if the requested algorithm is not available from the provider
NoSuchProviderException
- if the provider has not been configured
IllegalArgumentException
- if the provider name is null or emptypublic static long makeSeedSecure(SecureRandom random) throws IllegalArgumentException
NumberUtil.bytesBigEndianToLong
( random.generateSeed
(8) )
.
IllegalArgumentException
- if random == nullpublic static long makeSeedUnique()
System.nanoTime
to determine the low order bits of the result;
this should be different each time the JVM is run (assuming that the system time is different)
Warnings:
public static ec.util.MersenneTwisterFast get()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |