Last active
November 14, 2020 01:30
-
-
Save redfast00/4676630d285ebd27ef92 to your computer and use it in GitHub Desktop.
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
import math | |
class TIprng(object): | |
def __init__(self): | |
self.mod1 = 2147483563 | |
self.mod2 = 2147483399 | |
self.mult1 = 40014 | |
self.mult2 = 40692 | |
self.seed1 = 12345 | |
self.seed2 = 67890 | |
def seed(self, n): | |
n = math.fabs(math.floor(n)) | |
if (n == 0): | |
self.seed1 = 12345 | |
self.seed2 = 67890 | |
else: | |
self.seed1 = (self.mult1 * n) % self.mod1 | |
self.seed2 = (n)% self.mod2 | |
def rand(self, times = 0): | |
# like TI, this will return a list (array in python) if times == 1, | |
# or an integer if times isn't specified | |
if not(times): | |
self.seed1 = (self.seed1 * self.mult1) % self.mod1 | |
self.seed2 = (self.seed2 * self.mult2)% self.mod2 | |
result = (self.seed1 - self.seed2)/self.mod1 | |
if(result<0): | |
result = result+1 | |
return result | |
else: | |
return [self.rand() for _ in range(times)] | |
def randInt(self, minimum, maximum, times = 0): | |
# like TI, this will return a list (array in python) if times == 1, | |
# or an integer if times isn't specified | |
if not(times): | |
if (minimum < maximum): | |
return (minimum + math.floor((maximum- minimum + 1) * self.rand())) | |
else: | |
return (maximum + math.floor((minimum - maximum + 1) * self.rand())) | |
else: | |
return [self.randInt(minimum, maximum) for _ in range(times)] | |
def randBin(self, numtrials, prob, times = 0): | |
if not(times): | |
return sum([(self.rand() < prob) for _ in range(numtrials)]) | |
else: | |
return [self.randBin(numtrials, prob) for _ in range(times)] | |
def randM(self, rows, collumns): | |
# this will return an array of arrays | |
matrixArr = [[0 for x in range(collumns)] for x in range(rows)] | |
# we go from bottom to top, from right to left | |
for row in reversed(range(rows)): | |
for collumn in reversed(range(collumns)): | |
matrixArr[row][collumn] = self.randInt(-9, 9) | |
return matrixArr | |
testPRNG = TIprng() | |
testPRNG.seed(0) | |
print(testPRNG.randInt(0,100)) | |
testPRNG.seed(0) | |
print(testPRNG.randM(3,4)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment