C# Class RandomOps.ThreadSafe.Wrapper

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.
Inheritance: System.Random
Exibir arquivo Open project: DanWBR/dwsim3

Public Methods

Method Description
Bool ( ) : bool

Thread-safe wrapper for Bool().

Byte ( ) : byte

Thread-safe wrapper for Byte().

Bytes ( int length ) : byte[]

Thread-safe wrapper for Bytes().

Enter ( ) : void

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.

Exit ( ) : void

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.

Gauss ( ) : double

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().

Uniform ( ) : double

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.

Wrapper ( Random rand ) : System

Construct the thread-safe RNG wrapper.

Method Details

Bool() public final method

Thread-safe wrapper for Bool().
public final Bool ( ) : bool
return bool

Byte() public final method

Thread-safe wrapper for Byte().
public final Byte ( ) : byte
return byte

Bytes() public final method

Thread-safe wrapper for Bytes().
public final Bytes ( int length ) : byte[]
length int Number of random bytes to return.
return byte[]

Enter() public method

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 Enter ( ) : void
return void

Exit() public method

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 Exit ( ) : void
return void

Gauss() public final method

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 final Gauss ( ) : double
return double

Uniform() public final method

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 final Uniform ( ) : double
return double

Wrapper() public method

Construct the thread-safe RNG wrapper.
public Wrapper ( Random rand ) : System
rand System.Random The RNG to be made thread-safe.
return System