A random.org seeded random generator. : Random « Development Class « Java






A random.org seeded random generator.

  
/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

//package org.cspoker.common.util.random;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.security.SecureRandom;
import java.util.Random;

//import org.apache.log4j.Logger;

/**
 * A random.org seeded random generator.
 * 
 * @author Kenzo
 * 
 */
public class RandomOrgSeededRandomGenerator implements RandomSource {
  //private static Logger logger = Logger
    //  .getLogger(RandomOrgSeededRandomGenerator.class);

  private static RandomOrgSeededRandomGenerator instance = new RandomOrgSeededRandomGenerator();

  private Random random;

  /**
   * Construct a new random.org seeded random generator.
   * 
   */
  private RandomOrgSeededRandomGenerator() {

    try {
      final URL url = new URL(
          "http://www.random.org/strings/?num=10&len=10&digits=on&unique=on&format=plain&rnd=new");
      URLConnection connection = url.openConnection();
      connection.setConnectTimeout(5000);
      final BufferedReader in = new BufferedReader(new InputStreamReader(
          connection.getInputStream()));

      final StringBuilder stringBuilder = new StringBuilder();

      String line;
      while ((line = in.readLine()) != null) {
        stringBuilder.append(line);
      }

      in.close();

      final byte[] seed = stringBuilder.toString().getBytes();

      random = new SecureRandom(seed);
    } catch (final MalformedURLException e) {
      //logger.info("Default secure random is used.");
      random = new SecureRandom();
    } catch (final IOException e) {
      //logger.info("Default secure random is used.");
      random = new SecureRandom();
    }
  }

  public static RandomOrgSeededRandomGenerator getInstance() {
    return RandomOrgSeededRandomGenerator.instance;
  }

  /**
   * Returns a random-object.
   * 
   * The default implementation uses the current time as seed.
   * 
   * @return A random-object.
   */
  public Random getRandom() {
    return random;
  }
}

   
    
  








Related examples in the same category

1.Create random number
2.Create two random number generators with the same seed
3.Does Math.random() produce 0.0 and 1.0
4.Random numbers between 1 and 100
5.Random number between 0 AND 10
6.Random integers that range from from 0 to n
7.Random.nextInt(n) returns a distributed int value between 0 (inclusive) and n (exclusive).
8.Round Java float and double numbers using Math.round
9.Randomizer
10.nextDouble() and nextGaussian() in java.util.Random
11.Generating random numbers
12.Generate random ints by asking Random() for
13.Getting random numbers: nextGaussian
14.Generate a random array of numbers
15.Next double and next int
16.Random bytes
17.Random boolean
18.Random long type number
19.Random float type number
20.Random double type number
21.Math.random
22.A wrapper that supports all possible Random methods via the java.lang.Math#random() method and its system-wide {@link Random} object.
23.Operations for random Strings
24.Random Util with ReentrantLock
25.A Java implementation of the MT19937 (Mersenne Twister) pseudo random number generator algorithm
26.Randomizer
27.Random: Properly synchronized and can be used in a multithreaded environment
28.A predictable random number generator.
29.Random GUID
30.Generates a secure random word with the given length consisting of uppercase and lowercase letters and numbers.
31.A random choice maker
32.Memory-efficient map of keys to values with list-style random-access semantics.
33.Generates a random integer inside the lo and hi interval
34.Randomizer
35.RandomGUID generates truly random GUIDs
36.A random access text file allows a text file to be accessed randomly starting at any position.
37.A garbage-free randomised quicksort
38.A linear random method based on xor shifts
39.Mersenne Twister Random
40.A GaussianHat is a random number generator that will give you numbers based on a population mean and standard deviation.
41.Randomly permutes the elements of the specified array
42.generate string consists of random characters.
43.Atomic Pseudo Random
44.Atomic Simple Random
45.Mersenne
46.Mersenne Twister
47.Mersenne Twister Fast
48.Mersenne Twister algorithm
49.Emulates a dice