-
Notifications
You must be signed in to change notification settings - Fork 0
/
truerandom.py
91 lines (73 loc) · 2.04 KB
/
truerandom.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
91
from urllib.request import urlretrieve
import time
from argcheck import arg_check_type, arg_check_in
from threading import Thread
BASE_2 = 0x02
BASE_6 = 0x06
BASE_8 = 0x08
BASE_10 = 0x0A
BASE_16 = 0x10
def Get(min, max, count=1, base=BASE_10):
arg_check_type((min, max), (int, int))
base = arg_check_in(base, (BASE_2, BASE_6, BASE_8, BASE_10, BASE_16))
if count < 1:
return
data = urlretrieve("https://www.random.org/integers/?num=%d&min=%d&max=%d&col=1&base=%d&format=plain&rnd=new" % (count, min, max, base))
f = open(data[0], "r")
data = f.readlines()
result = []
for i in data:
i.replace('\n', '')
result.append(int(i, base))
if count > 1:
return tuple(result)
else:
return result[0]
def RandomInteger(min, max, count=1, kwargs={}):
pass
def DebugLatency(count=10, text=False):
start = 0
avg_lat = 0
for i in range(0, count):
start = time.time()
Get(**kwargs)
diff = time.time() - start
avg_lat += diff
if text:
print(str(diff) + 's')
avg_lat /= count
if text:
print("Average latency of: " + str(avg_lat) + "s")
return avg_lat
_maxCount = 1000
_index = 0
_running = False
def nextInt():
global _index
global _currentVals
if not _running:
return None
val = _currentVals[_index]
_index += 1
return val
def stopGenerator():
global _running
_running = False
def _randThread():
global _running
global _currentVals
_currentVals = list(Get(0, 100, count=_maxCount))
_running = True
while _running:
if _index > _maxCount / 2:
newVals = list(Get(0, 100, count=_maxCount))
_currentVals.reverse()
for i in range(0, int(_maxCount / 2) - 1):
_currentVals.pop()
_currentVals.reverse()
_currentVals = newVals
time.sleep(0.05)
_rand_thread = Thread(target=_randThread)
_rand_thread.start()
del _randThread
del _rand_thread