• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# G P S V

ostapdontstop May 20th, 2020 (edited) 1,125 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import tkinter
2.
3. from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
4. from matplotlib.figure import Figure
5. import matplotlib.pyplot as plt
6.
7. import random
8.
9.
10. def mid_square(init):
11.     """Middle-square generator"""
12.     depth = len(str(init))
13.     start = depth // 2
14.     init_seed = last = init
15.
16.     while True:
17.         last = init
18.         square_str = str(init * init).zfill(depth*2)
19.         mid_str = square_str[start:start+depth]
20.         init = int(mid_str)
21.         if init == last :
22.             init = init_seed
23.         else :
24.             yield float('0.'+ mid_str)
25.
26. def mid_multi(init):
27.     """Middle-multiplication generator"""
28.     depth = len(str(init))
29.     start = depth // 2
30.     init_seed = last = init
31.
32.     while True:
33.         multi_str = str(last * init).zfill(depth*2)
34.         mid_str = multi_str[start:start+depth]
35.         last = init
36.         init = int(mid_str)
37.         if init == last :
38.             init = last = init_seed
39.         else :
40.             yield float('0.'+ mid_str)
41.
42.
43. def mix_sum(init):
44.     """Mixing generator"""
45.     l = len(str(init))
46.     div = 10**(l-2)
47.     mod = 100
48.     while True:
49.         beg = init // div
50.         end = init % mod
51.
52.         left = init // mod
53.         right = init % div
54.
55.         a = right * mod + beg
56.         b = end * div + left
57.         init = a + b
58.         yield float('0.'+ str(init)[-l:])
59.
60.
61.
62. def lcg(modulus, a, c, seed):
63.     """Linear congruential generator"""
64.     while True:
65.         seed = (a * seed + c) % modulus
66.         yield seed / modulus
67.
68.
69.
70. lcg_gen = lcg(2**32, 1664525, 1013904223, 0)
71. mid_square_gen = mid_square(36149212)
72. mid_multi_gen = mid_multi(111111)
73. mix_sum_gen = mix_sum(12)
74.
75.
76.
77.
78. root = tkinter.Tk()
79. root.wm_title("Embedding in Tk")
80.
81.
82. fig, ax = plt.subplots(2,1,sharey=True, tight_layout=True)
83. fig.canvas.set_window_title('Графики ГПСЧ')
84.
85.
86. canvas = FigureCanvasTkAgg(fig, master=root)
87. canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
88.
89.
90. bins = 20
91. n = 1000
92. CURRENT = 0
93.
94.
95. def method(title, fu) :
96.     return {'title': title, 'fu': fu}
97.
98. methods = []
99. methods.append(method('Срединные квадраты', lambda: next(mid_square_gen)))
100. methods.append(method('Срединные произведения', lambda: next(mid_multi_gen)))
101. methods.append(method('Метод перемешивания', lambda: next(mix_sum_gen)))
102. methods.append(method('Линейно конгруэнтный', lambda: next(lcg_gen)))
103. methods.append(method('random()', random.random))
104.
105.
106. def set_data(length, method = None):
107.
108.     global CURRENT
109.
110.     if method != None:
111.         CURRENT = method
112.
113.     ax[0].clear()
114.     ax[0].set_title(methods[-1]['title'])
115.     # ax[0].hist([methods[-1]['fu']() for gen in range(length)],bins)
116.     ax[0].scatter(range(length),[methods[-1]['fu']() for gen in range(length)],s=1)
117.
118.     ax[1].clear()
119.     ax[1].set_title(methods[CURRENT]['title'])
120.     # ax[1].hist([methods[CURRENT]['fu']() for gen in range(length)],bins)
121.     ax[1].scatter(range(length),[methods[CURRENT]['fu']() for gen in range(length)],s=1)
122.
123.     canvas.draw()
124.
125.
126.
127.
128. set_data(n)
129.
130.
131.
132.
133. message = tkinter.StringVar()
134.
135. def _input(event = None, set_gen = None):
136.
137.     try:
138.         n = int(message.get())
139.     except ValueError:
140.         return
141.
142.     if n < 0:
143.         return
144.
145.     if set_gen != None:
146.         CURRENT = int(set_gen)
147.
148.     set_data(n, set_gen)
149.
150.
151.
152. f_methods = tkinter.Frame()
153.
154. meth_buts = []
155. for i in range(len(methods)-1):
156.     x = i // 2
157.     y = i %  2
158.     meth_buts.append(tkinter.Button(f_methods,text=methods[i]['title'], width=22, command=lambda i=i: _input(set_gen = i)))
159.     meth_buts[i].grid(row=x, column=y)
160.
161.
162.
163. f_input = tkinter.Frame()
164.
165. label = tkinter.Label(f_input,text='Длина')
166. text = tkinter.Entry(f_input,textvariable=message)
167. button = tkinter.Button(f_input,text="OK", command=_input)
168.
169. button.config(width=4)
170. text.insert(tkinter.END, str(n))
171. text.bind('<Return>', _input)
172.
173.
174.