/// ------------------------------------------------------
/// RandomOps - (Pseudo) Random Number Generator For C#
/// Copyright (C) 2003-2010 Magnus Erik Hvass Pedersen.
/// Please see the file license.txt for license details.
/// RandomOps on the internet: http://www.Hvass-Labs.org/
/// ------------------------------------------------------
using System.Collections.Generic;
namespace RandomOps
{
///
/// Random Number Generator (RNG) that downloads random bytes from the internet
/// website: www.random.org. Not thread-safe.
/// These random bytes are generated from atmospheric noise picked up by radios.
/// Only a certain number of bits are allowed to be downloaded from this website
/// to your IP address each day, so this RNG is used with a Fallback-PRNG.
///
public class RandomDotOrg : ByteStream
{
#region Constructor.
///
/// Constructs the RNG-object.
///
/// Number of random bytes the buffer holds.
/// Fallback RNG to be used when buffer is empty.
/// Use fallback RNG for this many bytes before trying to fill buffer again.
public RandomDotOrg(int bufferSize, Random randFallback, int numFallback)
: base(bufferSize, randFallback, numFallback)
{
}
#endregion
#region Download random bytes from internet.
///
/// Do the actual retrieval of random bytes from the www.random.org website.
///
/// Number of bytes to retrieve (max 10000).
/// List of random bytes.
///
/// Thrown if daily download quota is depleted,
/// the HTTP response code is 503 for 'Server Unavailable'.
///
public static List RetrieveBytes(int length)
{
List bytes = new List(length);
string url = @"http://www.random.org/integers/?num=" + length + @"&min=0&max=255&col=1&base=10&format=plain&rnd=new";
string data = HtmlDownload.HtmlToString(url);
string[] dataArray = data.Split('\n');
foreach (string s in dataArray)
{
if (s != null && s != "")
{
byte b;
if (System.Byte.TryParse(s, out b))
{
bytes.Add(b);
}
}
}
return bytes;
}
#endregion
#region Base-class overrides.
///
/// Name of the RNG.
///
public override string Name
{
get { return "Random.Org Sync"; }
}
///
/// The www.random.org website only allows 10000 elements
/// to be retrieved at once.
///
protected override int MaxRetrieveLength
{
get { return 10000; }
}
///
/// Called from ByteStream to request the retrieval of random bytes.
///
/// The number of bytes requested.
protected sealed override void DoFillBuffer(int length)
{
// Retrieve the bytes.
IEnumerable bytes = RetrieveBytes(length);
// Add the bytes to the ByteStream-buffer.
base.AddBuffer(bytes);
}
#endregion
}
}