// // Math.NET Numerics, part of the Math.NET Project // http://numerics.mathdotnet.com // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com // // Copyright (c) 2009-2010 Math.NET // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation // files (the "Software"), to deal in the Software without // restriction, including without limitation the rights to use, // copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following // conditions: // // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. // using MathNet.Numerics.Properties; using System; namespace MathNet.Numerics.Random { /// /// Implements a multiply-with-carry Xorshift pseudo random number generator (RNG) specified in Marsaglia, George. (2003). Xorshift RNGs. /// Xn = a * Xn−3 + c mod 2^32 /// http://www.jstatsoft.org/v08/i14/paper /// public class Xorshift : AbstractRandomNumberGenerator { /// /// The default value for X1. /// private const uint YSeed = 362436069; /// /// The default value for X2. /// private const uint ZSeed = 77465321; /// /// The default value for the multiplier. /// private const uint ASeed = 916905990; /// /// The default value for the carry over. /// private const uint CSeed = 13579; /// /// The multiplier to compute a double-precision floating point number [0, 1) /// private const double UlongToDoubleMultiplier = 1.0 / (uint.MaxValue + 1.0); /// /// Seed or last but three unsigned random number. /// private ulong _x; /// /// Last but two unsigned random number. /// private ulong _y; /// /// Last but one unsigned random number. /// private ulong _z; /// /// The value of the carry over. /// private ulong _c; /// /// The multiplier. /// private readonly ulong _a; /// /// Initializes a new instance of the class using /// the current time as the seed. /// /// If the seed value is zero, it is set to one. Uses the /// value of to /// set whether the instance is thread safe. /// Uses the default values of: /// /// a = 916905990 /// c = 13579 /// X1 = 77465321 /// X2 = 362436069 /// public Xorshift() : this((int)DateTime.Now.Ticks) { } /// /// Initializes a new instance of the class using /// the current time as the seed. /// /// The multiply value /// The initial carry value. /// The initial value if X1. /// The initial value if X2. /// If the seed value is zero, it is set to one. Uses the /// value of to /// set whether the instance is thread safe. /// Note: must be less than . /// public Xorshift(long a, long c, long x1, long x2) : this((int)DateTime.Now.Ticks, a, c, x1, x2) { } /// /// Initializes a new instance of the class using /// the current time as the seed. /// /// if set to true , the class is thread safe. /// /// Uses the default values of: /// /// a = 916905990 /// c = 13579 /// X1 = 77465321 /// X2 = 362436069 /// public Xorshift(bool threadSafe) : this((int)DateTime.Now.Ticks, threadSafe) { } /// /// Initializes a new instance of the class using /// the current time as the seed. /// /// if set to true , the class is thread safe. /// The multiply value /// The initial carry value. /// The initial value if X1. /// The initial value if X2. /// must be less than . public Xorshift(bool threadSafe, long a, long c, long x1, long x2) : this((int)DateTime.Now.Ticks, threadSafe, a, c, x1, x2) { } /// /// Initializes a new instance of the class. /// /// The seed value. /// If the seed value is zero, it is set to one. Uses the /// value of to /// set whether the instance is thread safe. /// Uses the default values of: /// /// a = 916905990 /// c = 13579 /// X1 = 77465321 /// X2 = 362436069 /// public Xorshift(int seed) : this(seed, Control.ThreadSafeRandomNumberGenerators) { } /// /// Initializes a new instance of the class. /// /// The seed value. /// If the seed value is zero, it is set to one. Uses the /// value of to /// set whether the instance is thread safe. /// The multiply value /// The initial carry value. /// The initial value if X1. /// The initial value if X2. /// must be less than . public Xorshift(int seed, long a, long c, long x1, long x2) : this(seed, Control.ThreadSafeRandomNumberGenerators, a, c, x1, x2) { } /// /// Initializes a new instance of the class. /// /// The seed value. /// if set to true, the class is thread safe. /// /// Uses the default values of: /// /// a = 916905990 /// c = 13579 /// X1 = 77465321 /// X2 = 362436069 /// public Xorshift(int seed, bool threadSafe) : base(threadSafe) { if (seed == 0) { seed = 1; } _x = (uint)seed; _y = YSeed; _z = ZSeed; _c = CSeed; _a = ASeed; } /// /// Initializes a new instance of the class. /// /// The seed value. /// if set to true, the class is thread safe. /// The multiply value /// The initial carry value. /// The initial value if X1. /// The initial value if X2. /// must be less than . public Xorshift(int seed, bool threadSafe, long a, long c, long x1, long x2) : base(threadSafe) { if (seed == 0) { seed = 1; } if (a <= c) { throw new ArgumentException(string.Format(Resources.ArgumentOutOfRangeGreater, "a", "c"), "a"); } _x = (uint)seed; _y = (ulong)x1; _z = (ulong)x2; _a = (ulong)a; _c = (ulong)c; } /// /// Returns a random number between 0.0 and 1.0. /// /// /// A double-precision floating point number greater than or equal to 0.0, and less than 1.0. /// protected override double DoSample() { var t = (_a * _x) + _c; _x = _y; _y = _z; _c = t >> 32; _z = t & 0xffffffff; return _z * UlongToDoubleMultiplier; } } }