/// ------------------------------------------------------ /// 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; using System.Threading; namespace RandomOps.ThreadSafe { /// /// Wrapper for an RNG that makes the calls to Uniform(), Bool(), Byte(), and /// Bytes() thread-safe by locking the object. Note that it is the ThreadSafe /// object that is being locked and not the RNG object it wraps around, so calls /// to methods should always be made to the ThreadSafe-object and not to the /// RNG object directly. This works well for infrequent access to the RNG but /// for frequent access you should use e.g. ThreadSafe.MWC256 instead for /// performance reasons. /// /// /// Derived calls are also thread-safe because they in turn call Uniform() /// and the other base-methods. If you use a custom RNG that have additional /// methods available which do not indirectly call one of the base-methods, /// then you must implement similar thread-safe wrappers. The RNG-object can /// potentially be locked several times by one thread because of how e.g. /// Bool() may call Uniform(), and such nested locking is supported in C#. /// It may be possible to implement more efficient locks for certain RNG's, /// e.g. for Ran2 we could instead implement a threadsafe version of the /// internal Rand() method. /// public partial class Wrapper : Random { #region Constructor. /// /// Construct the thread-safe RNG wrapper. /// /// The RNG to be made thread-safe. public Wrapper(Random rand) { _rand = rand; } #endregion #region Public methods. /// /// Same as a call to Monitor.Enter() on the RNG-object (not the /// ThreadSafe-object). Useful if you need to generate several random /// numbers in a batch without having to reacquire the lock for /// each call to the RNG. /// public void Enter() { Monitor.Enter(this); } /// /// Same as a call to Monitor.Exit() on the RNG-object (not the /// ThreadSafe-object). Must be called once for each call to Enter() /// to release the lock again. /// public void Exit() { Monitor.Exit(this); } #endregion #region Internal variables. /// /// The non-thread-safe RNG used for generating the numbers. /// Random _rand; #endregion #region Base-class Overrides. /// /// Name of the RNG. /// public override string Name { get { return "ThreadSafe(" + _rand.Name + ")"; } } /// /// Thread-safe wrapper for Uniform(). Note that derived methods /// that rely on Uniform() to create their random numbers, e.g. /// Gauss(), will automatically also be thread-safe. /// public sealed override double Uniform() { double value; lock (this) { value = _rand.Uniform(); } return value; } /// /// Thread-safe wrapper for Bool(). /// public sealed override bool Bool() { bool b; lock (this) { b = _rand.Bool(); } return b; } /// /// Thread-safe wrapper for Byte(). /// public sealed override byte Byte() { byte b; lock (this) { b = _rand.Byte(); } return b; } /// /// Thread-safe wrapper for Bytes(). /// /// Number of random bytes to return. /// Array of random bytes. public sealed override byte[] Bytes(int length) { byte[] arr; lock (this) { arr = _rand.Bytes(length); } return arr; } /// /// Thread-safe wrapper for Gauss(). /// /// /// The default implementation calls Uniform() a number of times. /// This wrapper therefore locks the RNG-object for the entirety /// of these multiple calls, instead of locking for each separate /// call to Uniform(). /// public sealed override double Gauss() { double value; lock (this) { value = _rand.Gauss(); } return value; } #endregion } }