Python Task on War and Peace by Leo Tolstoy

#!/bin/python3

“””
Explain how your pseudo random numbers are produced.
– The pseudo random numbers without seed value. Will be take the nanosecond on posis system. Then move to text file base on position. So it will work.
– The pseudo rando number with seed value. Will be move to file with this position. That’s mean the random will be the same
“””

```import time
from statistics import mean

class WarAndPeacePseudoRandomNumberGenerator:
def __init__(self, seed=None):
""" Constructor """
self.seed = seed
self.filename = 'war-and-peace.txt'
#self.file = open(self.filename, 'rb')
self.file = None
self.currentTime = self.getCurrentTimeNanosecond()
#self.currentPosition = 0

def getCurrentTimeNanosecond(self):
return int(round(time.time() * (10 ** 9)))
#return time.time_ns()

def getRandomValue(self):
""" Get randome value from 0 to 999999 base on seed value. If seed value, get randome base on time-stamp"""
tmp = None
if self.seed is None:
timeStr = str(self.getCurrentTimeNanosecond())
tmp = int(timeStr[-5:])
else:
#tmp = self.seed + (self.getCurrentTimeNanosecond() - self.currentTime)
tmp = self.seed
return (1 * tmp + 3123) % 999999

def getCharacterAt(self, indexVal):
""" Get character of file at indexVal """
binarystr = " "
result = None
self.file = open(self.filename, 'rb')
while binarystr.isspace():
self.file.seek(indexVal)
#self.currentPosition += int(indexVal)
indexVal += 1
try:
result = binarystr.decode("ascii")
except UnicodeError:
binarystr = " "
self.file.close()
return result

def convertBinaryStrToNum(self, binaryStr):
""" Convert binary string to number """
result = 0.0
divisor = 2
for i in range(len(binaryStr)):
result += int(binaryStr[i]) * (1 / (divisor))
divisor = divisor * 2
return result

def random(self):
""" Get random number using the war-and-peace.txt"""
randomNum = self.getRandomValue()
binaryStr = ""
while len(binaryStr) < 16:
start = self.getCharacterAt(randomNum)
end = self.getCharacterAt(randomNum + self.getRandomValue())
bit = None
#print("DEBUG: start: {} - end: {}".format(start, end))
if start > end:
bit = "1"
if start < end:
bit = "0"
if bit is None:
randomNum = randomNum + self.getRandomValue()
continue
randomNum += self.getRandomValue()
binaryStr += bit
#print("Test binstr: {}".format(binaryStr))
return self.convertBinaryStrToNum(binaryStr)

def closeFile(self):
self.file.close()

if __name__ == "__main__":
print("With default constructor: WarAndPeacePseudoRandomNumberGenerator()")
prng = WarAndPeacePseudoRandomNumberGenerator()
list1 = []
for i in range(10000):
#for i in range(10):
list1.append(prng.random())
#prng.closeFile()
print("median: {} ".format(mean(list1)))
print("minimum: {} ".format(min(list1)))
print("maximum: {} ".format(max(list1)))
print("")
print("With seed 12345 constructor: WarAndPeacePseudoRandomNumberGenerator(12345)")
prng2 = WarAndPeacePseudoRandomNumberGenerator(12345)
a = prng2.random()
b = prng2.random()
# #prng2.closeFile()
print("WarAndPeacePseudoRandomNumberGenerator(12345) - random: {} {}".format(a, b))

# prng3 = WarAndPeacePseudoRandomNumberGenerator(1234567)
# a = prng3.random()
# b = prng3.random()
# #prng3.closeFile()
# print("WarAndPeacePseudoRandomNumberGenerator(1234567) - random: {} {}".format(a, b))
list2 = []
for i in range(10000):
list2.append(prng2.random())
print("median: {} ".format(mean(list2)))
print("minimum: {} ".format(min(list2)))
print("maximum: {} ".format(max(list2)))```