/// ------------------------------------------------------
/// 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
{
///
/// Similar to the RandomDotOrg-class, only this uses asynchronous retrieval
/// of the random bytes, which is preferrable in time-critical applications.
/// If bytes are not available in the downloaded buffer, then a thread is
/// woken up to fill the buffer. In the meantime a Fallback RNG is used instead.
/// Not thread-safe.
///
///
/// Be sure to call Dispose() after you are done using this RNG,
/// so the worker-thread can be shut down properly. Also note
/// that this class is not thread-safe with regard to multiple
/// threads calling e.g. the Uniform() method simultaneously.
/// To make it thread-safe in this manner, wrap it in a
/// ThreadSafe-object.
///
public class RandomDotOrgAsync : ByteStreamAsync
{
#region Constructor.
///
/// Constructs the RNG-object.
///
/// Number of random bytes the buffer holds.
/// Refill buffer asynchronously when its size falls below this.
/// Fallback RNG to be used when buffer is empty.
/// Use fallback RNG for this many bytes before trying to fill buffer again.
public RandomDotOrgAsync(int bufferSize, int retrieveTrigger, Random randFallback, int numFallback)
: base(bufferSize, retrieveTrigger, randFallback, numFallback)
{
}
#endregion
#region Base-class overrides.
///
/// Name of the RNG.
///
public override string Name
{
get { return "Random.Org Async"; }
}
///
/// 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)
{
// Re-use the RandomDotOrg.RetrieveBytes() method.
IEnumerable bytes = RandomDotOrg.RetrieveBytes(length);
AddBuffer(bytes);
}
#endregion
}
}