/// <summary> /// Returns the composition of two linear congruential functions: /// g(f(x)). Assumes m is the same. /// </summary> /// <param name="f"></param> /// <param name="g"></param> /// <returns></returns> public static LCG Compose(LCG f, LCG g) { if (f.M != g.M) { throw new ArgumentException("m is not the same"); } // g(f(x)) = g(ax + b) = c(ax + b) + d = (ca)x + (cb + d) (mod m) return(new LCG(f.A * g.A, (f.B * g.A) + g.B, f.M)); }
public LCG GetInverse() { // f(x) = A*x + b (mod m) // f^(-1)(x) = var isInvertible = ModHelper.TryGetModInverse(A, M, out BigInteger aInverse); if (!isInvertible) { throw new Exception($"{A} is not invertible mod {M}"); } var result = new LCG(aInverse, -1 * B * aInverse, M); return(result); }
public override bool Equals(Object obj) { //Check for null and compare run-time types. if ((obj == null) || !this.GetType().Equals(obj.GetType())) { return(false); } else { LCG other = (LCG)obj; return((A == other.A) && (B == other.B) && (M == other.M)); } }