/// ------------------------------------------------------ /// 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; namespace RandomOps { /// /// Summing the output of multiple RNGs and taking modulo 2^32. /// Note that this assumes the RNGs have RandMax roughly equal /// to UInt32.MaxValue otherwise there will be a bias. Thread-safe /// if RNGs are 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 partial class SumUInt32 : RanUInt32 { #region Constructor. /// /// Constructs the RNG-object from different RNG's. /// public SumUInt32(RanUInt32[] rands) : base() { Rands = rands; } #endregion #region Internal variables. /// /// The array of RNGs to sum. /// protected Random[] Rands { get; private set; } #endregion #region Base-class overrides. /// /// Name of the RNG. /// public override sealed string Name { get { string s = "Sum("; foreach (Random rand in Rands) { s += rand.Name + ", "; } s += ")"; return s; } } /// /// Draw a random number in inclusive range {0, .., RandMax} /// public override sealed UInt32 Rand() { UInt32 sum = 0; // Sum and modulo. foreach (RanUInt32 rand in Rands) { sum += rand.Rand(); } return sum; } /// /// The maximum possible value returned by Rand(). /// public override sealed UInt32 RandMax { get { return UInt32.MaxValue; } } #endregion } }