Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14155

Linear congruential generator

$
0
0

i want to check whether the selected parameters, a, c and m, will generate full period random number or not. i want to write the code for it. I wrote the code for it but it does not work correctly.the code is

def gcd(small, large):    if(large < small):      temp = large      large = small      small = temp    for i in range(small, 0, -1):        if large % i == 0 and small % i == 0:            return i# m and c must be co-primedef relative_prime_condition(c, m):    if gcd(c,m) == 1:        return True    return Falsedef is_prime(number):    for i in range(2, int(number/2) + 1):        if number % i == 0:            return False    return True# a-1 must divide all the prime factors of mdef prime_factor_condition(a, m):    for i in range(2, int(m/2) + 1):        if is_prime(i) and m % i == 0 and (a-1) % i != 0:            return False    return True# if m divides 4, then a-1 must divide 4    def num4_condition(a, m):    if m % 4 == 0 and (a-1) % 4 != 0:        return False    return Truedef LCG(seed, a, c, m):    random_numbers = [seed]    if relative_prime_condition(c,m) and prime_factor_condition(a,m) and num4_condition(c,m):        for i in range(1, m+1):            random_numbers.append((a * random_numbers[i-1] + c) % m)    else:        print("These Parameters are not able to generate the random numbers")        for i in range(1, 5):            random_numbers.append((a * random_numbers[i-1] + c) % m)    return random_numbersprint(LCG(seed=0, a=5, c=3, m=13))

this code displays[0, 3, 5, 2, 0, 3, 5, 2, 0, 3, 5, 2, 0, 3]

all three conditions are satisfied but don't know why it is not generating full period random numbers.


Viewing all articles
Browse latest Browse all 14155

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>