// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) public abstract class RNG { public abstract int GetInt(); public abstract int MaxInt { get; } public double GetDouble() { return (GetInt() / (double)MaxInt); } } public class LCG : RNG { private long a; private long b; private long c; private long seed; public LCG(int a, int b, int c, int seed) { this.a = a; this.b = b; this.c = c; this.seed = seed; } public override int GetInt() { seed = (a * seed + b) % c; return (int)seed; } public override int MaxInt { get { return (int)c; } } } public class MRG : RNG { private long[] a; private long b; private long c; private long[] seed; public MRG(int[] a, int b, int c, int[] seed) { this.a = new long[a.Length]; for(int i = 0; i < a.Length; i++) this.a[i] = a[i]; this.b = b; this.c = c; this.seed = new long[seed.Length]; for(int i = 0; i < seed.Length; i++) this.seed[i] = seed[i]; } public override int GetInt() { long tmp = 0; for(int i = 0; i < a.Length; i++) { tmp = tmp + a[i] * seed[i]; } tmp = tmp + b; tmp = tmp % c; for(int i = 1; i < seed.Length; i++) { seed[i] = seed[i-1]; } seed[0] = tmp; return (int)seed[0]; } public override int MaxInt { get { return (int)c; } } } public class MLCG : LCG { public MLCG(int a, int c, int seed) : base(a, 0, c, seed) { } } // Minimal Standard algorithm // // Original proposed by Lewis, Goodman & Miller 1969, but popularized // by Park & Miller CACM Oct 1988. // // Name indicate that one should never choose an algorithm worse than this. // // cycle = 2 147 483 647 = 2^31-1 public class MinStd : MLCG { public MinStd(int seed) : base(16807, 2147483647, seed) { } } // L'Ecuyer algorithm // // Ecuyer & Blouin & Coutre ACM Simulations 1993. // // cycle = approx. 1E46 public class LEcuyer : MRG { public LEcuyer(int[] seed) : base(new int[] { 107374182, 0, 0, 0, 104480 }, 0, 2147483647, seed) { } } public interface Combiner { int Combine(int[] num); int MaxInt { get; } } public class Combined : RNG { private RNG[] g; private Combiner cmbn; public Combined(RNG[] g, Combiner cmbn) { this.g = g; this.cmbn = cmbn; } public override int GetInt() { int[] num = new int[g.Length]; for(int i = 0; i < num.Length; i++) { num[i] = g[i].GetInt(); } return cmbn.Combine(num); } public override int MaxInt { get { return cmbn.MaxInt; } } } // Uniform32 algorithm // // L'Ecuyer CACM Jun 1988. // // cycle = approx. 2.31E18 internal class Uniform32Combiner : Combiner { public int Combine(int[] num) { long tmp = num[0] - num[1]; tmp = (tmp + 2147483562) % 2147483562; return (int)tmp; } public int MaxInt { get { return 2147483562; } } } public class Uniform32 : Combined { public Uniform32(int seed1, int seed2) : base(new RNG[] { new MLCG(40014, 2147483563, seed1), new MLCG(40692, 2147483399, seed2) }, new Uniform32Combiner()) { } } // CMRG algorithm // // L'Ecuyer Operations Research 1996. // // cycle = 2^205 = approx. 1E61 internal class CMRGCombiner : Combiner { public int Combine(int[] num) { long tmp = num[0] - num[1]; tmp = (tmp + 2147483647) % 2147483647; return (int)tmp; } public int MaxInt { get { return 2147483647; } } } public class CMRG : Combined { public CMRG(int[] seed1, int[] seed2) : base(new RNG[] { new MRG(new int[] { 0, 63308, -183326 }, 0, 2147483647, seed1), new MRG(new int[] { 86098, 0, -539608 }, 0, 2145483479, seed2) }, new CMRGCombiner()) { } }