-
Notifications
You must be signed in to change notification settings - Fork 0
/
rng_multithread.py
61 lines (42 loc) · 1.38 KB
/
rng_multithread.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
import multiprocessing
import sys
import os
from PIL import ImageGrab
import time
import Primes
import RNGutils
def get_seed_from_pixel():
image = ImageGrab.grab()
w, h = image.size
w_cord = round(time.time() * 1000) % w
h_cord = round(time.time() * 1000) % h
pixel_values = image.load()
pixel = pixel_values[w_cord, h_cord]
mean_pixel_value = sum(pixel, 0) / len(pixel)
return round(mean_pixel_value)
def worker(seed, length, threads, rnd_range):
primes = Primes.Primes()
rng = RNGutils.RNGutils()
with open("output.txt", "a+") as file:
[file.write(f'{number}\n') for number in rng.sequence(primes=primes, pixel_seed=seed, length=int(length / threads),
rnd_range=rnd_range)]
file.close()
return
def main():
try:
os.remove('output.txt')
print('removed output file!')
except Exception as e:
print(f'Error: {e}')
start = time.time()
processes = []
for i in range(int(sys.argv[3])):
p = multiprocessing.Process(target=worker, args=(get_seed_from_pixel(), int(sys.argv[1]), int(sys.argv[3]), int(sys.argv[2])))
processes.append(p)
p.start()
for process in processes:
process.join()
end = time.time()
print(f'{sys.argv[1]} random numbers sequence generated in {end - start}')
if __name__ == '__main__':
main()