Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- import matplotlib.mlab as mlab
- import matplotlib.pyplot as plt
- import random
- def lcg(modulus, a, c, seed):
- """Linear congruential generator"""
- while True:
- seed = (a * seed + c) % modulus
- yield seed / modulus
- lcg_gen = lcg(2**32, 1664525, 1013904223, 0)
- n_bins = 50
- fig, axs = plt.subplots(2, 1, sharey=True, tight_layout=True)
- plt.ion()
- plt.show(block=False)
- ROOT = tk.Tk()
- ROOT.withdraw()
- while True:
- USER_INP = tk.simpledialog.askstring(title="ГПСЧ", prompt="Длина: ")
- if type(USER_INP) is not str:
- plt.show(block=True)
- break
- try:
- n = int(USER_INP)
- if n <= 0:
- raise ValueError
- except ValueError:
- tk.messagebox.showinfo("Ошибка!", "Неарвильный ввод. Необходимо целое число")
- continue
- x1 = [random.random() for i in range(n)]
- x2 = [next(lcg_gen) for i in range(n)]
- axs[0].clear()
- axs[0].hist(x1, bins=n_bins)
- axs[0].set_title('random() '+ str(n))
- axs[1].clear()
- axs[1].hist(x2, bins=n_bins)
- axs[1].set_title('lcg() '+ str(n))
- plt.draw()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement