Class RandomDotOrgSeedGenerator

java.lang.Object
org.uncommons.maths.random.RandomDotOrgSeedGenerator
All Implemented Interfaces:
SeedGenerator

public class RandomDotOrgSeedGenerator extends 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 the DevRandomSeedGenerator where 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 Details

    • BASE_URL

      private static final String BASE_URL
      See Also:
    • RANDOM_URL

      private static final String RANDOM_URL
      The URL from which the random bytes are retrieved.
      See Also:
    • USER_AGENT

      private static final 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:
    • cacheLock

      private static final Lock cacheLock
    • cache

      private static byte[] cache
    • cacheOffset

      private static int cacheOffset
  • Constructor Details

    • RandomDotOrgSeedGenerator

      public RandomDotOrgSeedGenerator()
  • Method Details

    • generateSeed

      public byte[] generateSeed(int length) throws SeedException
      Generate a seed value for a random number generator.
      Specified by:
      generateSeed in interface SeedGenerator
      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 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:
      IOException - If there is a problem downloading the random bits.
    • toString

      public String toString()
      Overrides:
      toString in class Object