//
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
//
// Copyright (c) 2009-2014 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 System;
using System.Collections.Generic;
using System.Runtime.Serialization;
#if !NETSTANDARD1_3
using System.Runtime;
#endif
namespace IStation.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
///
[Serializable]
[DataContract(Namespace = "urn:IStation/Numerics/Random")]
public class Xorshift : RandomSource
{
///
/// The default value for X1.
///
const uint YSeed = 362436069;
///
/// The default value for X2.
///
const uint ZSeed = 77465321;
///
/// The default value for the multiplier.
///
const uint ASeed = 916905990;
///
/// The default value for the carry over.
///
const uint CSeed = 13579;
///
/// The multiplier to compute a double-precision floating point number [0, 1)
///
const double UlongToDoubleMultiplier = 1.0/4294967296.0; // 1.0/(uint.MaxValue + 1.0)
///
/// Seed or last but three unsigned random number.
///
[DataMember(Order = 1)]
ulong _x;
///
/// Last but two unsigned random number.
///
[DataMember(Order = 2)]
ulong _y;
///
/// Last but one unsigned random number.
///
[DataMember(Order = 3)]
ulong _z;
///
/// The value of the carry over.
///
[DataMember(Order = 4)]
ulong _c;
///
/// The multiplier.
///
[DataMember(Order = 5)]
readonly ulong _a;
///
/// Initializes a new instance of the class using
/// a seed based on time and unique GUIDs.
///
/// 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(RandomSeed.Robust())
{
}
///
/// Initializes a new instance of the class using
/// a seed based on time and unique GUIDs.
///
/// 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(RandomSeed.Robust(), a, c, x1, x2)
{
}
///
/// Initializes a new instance of the class using
/// a seed based on time and unique GUIDs.
///
/// 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(RandomSeed.Robust(), threadSafe)
{
}
///
/// Initializes a new instance of the class using
/// a seed based on time and unique GUIDs.
///
/// 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(RandomSeed.Robust(), 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("a must be greater than c.", nameof(a));
}
_x = (uint)seed;
_y = (ulong)x1;
_z = (ulong)x2;
_a = (ulong)a;
_c = (ulong)c;
}
///
/// Returns a random double-precision floating point number greater than or equal to 0.0, and less than 1.0.
///
protected sealed override double DoSample()
{
var t = (_a*_x) + _c;
_x = _y;
_y = _z;
_c = t >> 32;
_z = t & 0xffffffff;
return _z*UlongToDoubleMultiplier;
}
///
/// Returns a random 32-bit signed integer greater than or equal to zero and less than
///
protected sealed override int DoSampleInteger()
{
var t = (_a * _x) + _c;
_x = _y;
_y = _z;
_c = t >> 32;
_z = t & 0xffffffff;
uint uint32 = (uint)_z;
int int31 = (int)(uint32 >> 1);
if (int31 == int.MaxValue)
{
return DoSampleInteger();
}
return int31;
}
///
/// Fills the elements of a specified array of bytes with random numbers in full range, including zero and 255 ().
///
protected sealed override void DoSampleBytes(byte[] buffer)
{
for (var i = 0; i < buffer.Length; i++)
{
var t = (_a * _x) + _c;
_x = _y;
_y = _z;
_c = t >> 32;
_z = t & 0xffffffff;
buffer[i] = (byte)(_z % 256);
}
}
///
/// Fills an array with random numbers greater than or equal to 0.0 and less than 1.0.
///
/// Supports being called in parallel from multiple threads.
[CLSCompliant(false)]
public static void Doubles(double[] values, int seed, ulong a = ASeed, ulong c = CSeed, ulong x1 = YSeed, ulong x2 = ZSeed)
{
if (a <= c)
{
throw new ArgumentException("a must be greater than c.", nameof(a));
}
if (seed == 0)
{
seed = 1;
}
ulong x = (uint)seed;
for (int i = 0; i < values.Length; i++)
{
var t = (a*x) + c;
x = x1;
x1 = x2;
c = t >> 32;
x2 = t & 0xffffffff;
values[i] = x2*UlongToDoubleMultiplier;
}
}
///
/// Returns an array of random numbers greater than or equal to 0.0 and less than 1.0.
///
/// Supports being called in parallel from multiple threads.
[CLSCompliant(false)]
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public static double[] Doubles(int length, int seed, ulong a = ASeed, ulong c = CSeed, ulong x1 = YSeed, ulong x2 = ZSeed)
{
var data = new double[length];
Doubles(data, seed, a, c, x1, x2);
return data;
}
///
/// Returns an infinite sequence of random numbers greater than or equal to 0.0 and less than 1.0.
///
/// Supports being called in parallel from multiple threads, but the result must be enumerated from a single thread each.
[CLSCompliant(false)]
public static IEnumerable DoubleSequence(int seed, ulong a = ASeed, ulong c = CSeed, ulong x1 = YSeed, ulong x2 = ZSeed)
{
if (a <= c)
{
throw new ArgumentException("a must be greater than c.", nameof(a));
}
if (seed == 0)
{
seed = 1;
}
ulong x = (uint)seed;
while (true)
{
var t = (a*x) + c;
x = x1;
x1 = x2;
c = t >> 32;
x2 = t & 0xffffffff;
yield return x2*UlongToDoubleMultiplier;
}
}
}
}