Skip to content

Instantly share code, notes, and snippets.

@komasaru
Last active January 12, 2018 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save komasaru/df06b72faae10a71515d4ceccc2802f8 to your computer and use it in GitHub Desktop.
Save komasaru/df06b72faae10a71515d4ceccc2802f8 to your computer and use it in GitHub Desktop.
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