Class 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 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 Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.String BASE_URL  
      private static byte[] cache  
      private static java.util.concurrent.locks.Lock cacheLock  
      private static int cacheOffset  
      private static int MAX_REQUEST_SIZE
      Random.org does not allow requests for more than 10k integers at once.
      private static java.lang.String RANDOM_URL
      The URL from which the random bytes are retrieved.
      private static java.lang.String USER_AGENT
      Used to identify the client to the random.org service.
    • 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 void refreshCache​(int requiredBytes)  
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • 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
    • Constructor Detail

      • RandomDotOrgSeedGenerator

        public RandomDotOrgSeedGenerator()
    • Method Detail

      • 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 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:
        toString in class java.lang.Object