-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpseudo_random_num.py
90 lines (73 loc) · 2.16 KB
/
pseudo_random_num.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import time
import os
import sys
import traceback
from Wichmann_Hill_psg import generate_wh_rng
m1 = None
m2 = None
n = None
def usage():
print(
f'Usage: python {sys.argv[0]} <lower-limit> <upper-limit> <how-many>')
sys.exit(1)
def validate_args():
if len(sys.argv) < 4:
usage()
global m1, m2, n
try:
m1 = int(sys.argv[1])
m2 = int(sys.argv[2])
n = int(sys.argv[3])
if m2 < m1:
raise Exception("LimitsMismatch")
if m2-m1+1 < n:
raise Exception("LimitsMismatch")
except ValueError:
usage()
except Exception as e:
print(e)
print(traceback.format_exc())
usage()
def squeeze_range(m1, m2, r, float_mode=False):
assert (m1 <= m2)
assert (r >= 0 and r <= 1)
val = m1 + (m2-m1+1)*r
if float_mode:
return val
return int(val)
def check_or_set_initial_seed():
s1_0 = None
s2_0 = None
s3_0 = None
if os.path.exists("seed.val"):
with open("seed.val", "r") as f:
content = f.read()
s1_0, s2_0, s3_0 = (int(x) for x in content.split(","))
else:
t = time.perf_counter()
s1_0 = int(str(t-int(t)).replace(".", ""))
t = time.perf_counter()
s2_0 = int(str(t-int(t)).replace(".", ""))
t = time.perf_counter()
s3_0 = int(str(t-int(t)).replace(".", ""))
write_new_seed(f"{s1_0},{s2_0},{s3_0}")
return s1_0, s2_0, s3_0
def write_new_seed(s1, s2, s3, file_path="seed.val"):
with open(file_path, "w") as f:
f.write(f"{s1},{s2},{s3}")
def generate_pseudo_random(m1, m2, n):
dct = {}
count = 0
while count < n:
s1_0, s2_0, s3_0 = check_or_set_initial_seed()
s1, s2, s3, r = generate_wh_rng(s1_0, s2_0, s3_0)
write_new_seed(s1, s2, s3)
num = squeeze_range(m1, m2, r)
if num not in dct:
dct[num] = None
count = count + 1
return [x for x in dct.keys()]
if __name__ == "__main__":
validate_args()
for k in generate_pseudo_random(m1,m2,n):
print(k)