def __init__(self, target, rand=True, state=None): # see https://fr.wikipedia.org/wiki/Générateur_congruentiel_linéaire self.target = target self.nextcount = 0 self.lcg_m = target.targetscount if state is not None: self.previous = state[0] self.lcg_c = state[1] self.lcg_a = state[2] self.nextcount = state[3] elif rand and target.targetscount > 1: # X_{-1} self.previous = random.randint(0, self.lcg_m - 1) # GCD(c, m) == 1 self.lcg_c = random.randint(1, self.lcg_m - 1) while mathutils.gcd(self.lcg_c, self.lcg_m) != 1: self.lcg_c = random.randint(1, self.lcg_m - 1) # a - 1 is divisible by all prime factors of m mfactors = reduce(lambda x, y: x * y, set(mathutils.factors(self.lcg_m))) # a - 1 is a multiple of 4 if m is a multiple of 4. if self.lcg_m % 4 == 0: mfactors *= 2 self.lcg_a = mfactors + 1 else: self.previous = self.lcg_m - 1 self.lcg_a = 1 self.lcg_c = 1