/// ------------------------------------------------------ /// 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.Diagnostics; namespace RandomOps { /// /// Pseudo-Random Number Generator (PRNG) based on MWC256 by /// George Marsaglia. Period of this PRNG is about 2^8222. /// Not thread-safe. /// /// /// This is a translation of the C source-code published 2003-05-13 /// in the newsgroup comp.lang.c by George Marsaglia, published /// here with Marsaglia's authorization under the license in license.txt /// public class MWC256 : RanUInt32Array { #region Constructors. /// /// Constructs the PRNG-object without a seed. Remember /// to seed it before drawing random numbers. /// public MWC256() : base() { } /// /// Constructs the PRNG-object using the designated seed. /// This is useful if you want to repeat experiments with the /// same sequence of pseudo-random numbers. /// public MWC256(UInt32[] seed) : base(seed) { } /// /// Constructs the PRNG-object and uses another RNG for seeding. /// public MWC256(Random rand) : base(rand) { } #endregion #region Internal definitions and variables /// /// Iterator array. /// UInt32[] Q = new UInt32[256]; /// /// Carry variable. /// UInt32 C; /// /// Iteration counter. /// Byte Counter = 255; /// /// Is PRNG ready for use? /// bool IsReady = false; #endregion #region PRNG Implementation. /// /// Draw a random number in inclusive range {0, .., RandMax} /// public sealed override UInt32 Rand() { Debug.Assert(IsReady); UInt64 t = (UInt64)809430660 * Q[++Counter] + C; C = (UInt32)(t >> 32); UInt32 retVal = (UInt32)t; Q[Counter] = retVal; return retVal; } /// /// The maximum possible value returned by Rand(). /// public sealed override UInt32 RandMax { get { return UInt32.MaxValue; } } /// /// Length of seed-array. /// public sealed override int SeedLength { get { return 257; } } /// /// Seed with an array. /// public sealed override void Seed(UInt32[] seed) { Debug.Assert(seed.Length == SeedLength); // First seed is used for C. C = seed[0] % 809430660; // Remaining seeds are used for Q. for (int i = 1; i < SeedLength; i++) { Q[i-1] = seed[i]; } IsReady = true; } #endregion #region Base-class overrides. /// /// Name of the RNG. /// public override string Name { get { return "MWC256"; } } #endregion } }