-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtask3.py
75 lines (56 loc) · 1.57 KB
/
task3.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
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
from scipy.stats import norm
#M = 22695477
M = 101
C = 1
p = 2 ** 32
flag = True
def get_random(U, M, C, p):
U_new = (U * M + C) % p
return U_new / p, U_new
def get_random_array(length):
U = [0]
R = []
global M, C, p
for i in range(length - 1):
rand, U_new = get_random(U[-1], M, C, p)
R.append(rand)
U.append(U_new)
return R
def get_L():
numbers = []
U = [0]
global M, C, p, flag
R = []
rand, num = get_random(U[-1], M, C, p)
while num not in numbers:
U.append(num)
R.append(rand)
numbers.append(num)
rand, num = get_random(U[-1], M, C, p)
if len(R) > 10000 and flag:
print('Более 10000. Продолжить? Y/n')
if input() != 'Y':
return len(U)
else:
flag = False
return len(R)
print('Наибольшая длина неповторяющихся элементов: ', get_L())
b = 0.99
d = 0.01
N = int((norm.ppf((1 - b) / 2) / (2 * d)) ** 2)
random_array = get_random_array(N)
sns_plot = sns.distplot(random_array)
fig = sns_plot.get_figure()
plt.show()
x = [random_array[2 * i] for i in range(len(random_array) // 2)]
y = [random_array[2 * i + 1] for i in range(len(random_array) // 2)]
hist, x_edges, y_edges = np.histogram2d(x, y, bins=10)
plt.imshow(hist, extent=[x_edges[0], x_edges[-1], y_edges[0], y_edges[-1]])
plt.xticks(ticks=x_edges)
plt.xlabel('x')
plt.yticks(ticks=y_edges)
plt.ylabel('y')
plt.show()