Last active
January 12, 2018 01:59
-
-
Save komasaru/df06b72faae10a71515d4ceccc2802f8 to your computer and use it in GitHub Desktop.
Python script to generate random numbers with LCGs algorithm.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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