Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python script to generate random numbers with LCGs algorithm.
#! /usr/local/bin/python3.6
"""
Random number generatrion with LCG algorithm
"""
import sys
import traceback
class RndnumLcgs:
A = 1103515245 # Multiplier
C = 12345 # Increment
M = 2 ** 31 # Modulus
N = 1000 # Number to generate
def __init__(self):
self.r = self.C # Initialization of seed
def generate_rndnum(self):
try:
for i in range(self.N):
self.r = (self.A * self.r + self.C) % self.M
print("{:>10} ".format(self.r), end="")
if i % 5 == 4:
print()
print()
except Exception as e:
raise
if __name__ == '__main__':
try:
obj = RndnumLcgs()
obj.generate_rndnum()
except Exception as e:
traceback.print_exc()
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment