-
Notifications
You must be signed in to change notification settings - Fork 0
/
prng.py
38 lines (30 loc) · 943 Bytes
/
prng.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import base64
import math
import datetime
class PRNG():
def __init__(self):
self.s = 1234
self.p = 999979
self.q = 999983
self.m = self.p * self.q
def hash(self, x: any):
y = base64.encodebytes(bytes(str(x).encode('utf8')))
z = 0
for i, v in enumerate(y):
z += v * math.pow(128, i)
return z
def seed(self, x: any = datetime.datetime.now()):
y = 0
z = 0
while y % self.p == 0 or y % self.q == 0 or y == 0 or y == 1:
y = (self.hash(x) + z) % self.m
z += 1
self.s = y
[self.next() for _ in range(10)]
def next(self):
self.s = (self.s * self.s) % self.m
return self.s / self.m
def random(self, l: float = 0, r: float = 1):
return self.next() * (r - l) + l
def randsign(self) -> int:
return -1 if self.random() > .5 else 1