Package org.uncommons.maths.random
Class RandomDotOrgSeedGenerator
- java.lang.Object
-
- org.uncommons.maths.random.RandomDotOrgSeedGenerator
-
- All Implemented Interfaces:
SeedGenerator
public class RandomDotOrgSeedGenerator extends java.lang.Object implements SeedGenerator
Connects to the random.org website (via HTTPS) and downloads a set of random bits to use as seed data. It is generally better to use theDevRandomSeedGeneratorwhere possible, as it should be much quicker. This seed generator is most useful on Microsoft Windows and other platforms that do not provide /dev/random.
-
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.StringBASE_URLprivate static byte[]cacheprivate static java.util.concurrent.locks.LockcacheLockprivate static intcacheOffsetprivate static intMAX_REQUEST_SIZERandom.org does not allow requests for more than 10k integers at once.private static java.lang.StringRANDOM_URLThe URL from which the random bytes are retrieved.private static java.lang.StringUSER_AGENTUsed to identify the client to the random.org service.
-
Constructor Summary
Constructors Constructor Description RandomDotOrgSeedGenerator()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description byte[]generateSeed(int length)Generate a seed value for a random number generator.private voidrefreshCache(int requiredBytes)java.lang.StringtoString()
-
-
-
Field Detail
-
BASE_URL
private static final java.lang.String BASE_URL
- See Also:
- Constant Field Values
-
RANDOM_URL
private static final java.lang.String RANDOM_URL
The URL from which the random bytes are retrieved.- See Also:
- Constant Field Values
-
USER_AGENT
private static final java.lang.String USER_AGENT
Used to identify the client to the random.org service.
-
MAX_REQUEST_SIZE
private static final int MAX_REQUEST_SIZE
Random.org does not allow requests for more than 10k integers at once.- See Also:
- Constant Field Values
-
cacheLock
private static final java.util.concurrent.locks.Lock cacheLock
-
cache
private static byte[] cache
-
cacheOffset
private static int cacheOffset
-
-
Method Detail
-
generateSeed
public byte[] generateSeed(int length) throws SeedExceptionGenerate a seed value for a random number generator.- Specified by:
generateSeedin interfaceSeedGenerator- Parameters:
length- The length of the seed to generate (in bytes).- Returns:
- A byte array containing the seed data.
- Throws:
SeedException- If a seed cannot be generated for any reason.
-
refreshCache
private void refreshCache(int requiredBytes) throws java.io.IOException- Parameters:
requiredBytes- The preferred number of bytes to request from random.org. The implementation may request more and cache the excess (to avoid making lots of small requests). Alternatively, it may request fewer if the required number is greater than that permitted by random.org for a single request.- Throws:
java.io.IOException- If there is a problem downloading the random bits.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-