/// ------------------------------------------------------ /// 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/ /// ------------------------------------------------------ namespace RandomOps { /// /// Abstract class for using multiple RNGs that can be switched /// between. Example implementation is the Switcher-class which /// does the RNG-switching randomly. Thread-safe if supplied RNGs /// are thread-safe, and if SelectRand() is made thread-safe. /// /// /// If you are using RNGs that have custom methods for generating /// random numbers then you need to extend this class in a fashion /// similar to that of the Uniform()-method. /// public abstract partial class Multi : Random { #region Constructor. /// /// Constructs the RNG-object from different RNG's. /// /// The RNGs that will be switched between. public Multi(Random[] rands) : base() { Rands = rands; } #endregion #region Override these methods. /// /// Select the RNG to use. /// /// Index into the Rands-array. protected abstract int SelectRand(); #endregion #region Internal variables. /// /// The array of RNGs to switch between. /// protected Random[] Rands { get; private set; } /// /// The currently selected RNG. /// Random Rand; #endregion #region RNG Implementation. /// /// Switch the RNG currently being used. This is to be /// called before every RNG-method call. /// void Switch() { int selected = SelectRand(); Rand = Rands[selected]; } #endregion #region Base-class overrides. /// /// Name of the RNG. /// public override string Name { get { string s = "Multi("; foreach (Random rand in Rands) { s += rand.Name + ", "; } s += ")"; return s; } } /// /// Draw a uniform random number in the exclusive range (0,1) /// public sealed override double Uniform() { Switch(); return Rand.Uniform(); } /// /// Draw a random boolean with equal probability of drawing true or false. /// public sealed override bool Bool() { Switch(); return Rand.Bool(); } /// /// Draw a random and uniform byte. /// public sealed override byte Byte() { Switch(); return Rand.Byte(); } /// /// Draw an array of random and uniform bytes. /// /// The array length requested. public sealed override byte[] Bytes(int length) { Switch(); return Rand.Bytes(length); } #endregion } }