Advertisement
ostapdontstop

gpsv

May 15th, 2020
1,085
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.12 KB | None | 0 0
  1. import tkinter as tk
  2. import matplotlib.mlab as mlab
  3. import matplotlib.pyplot as plt
  4. import random
  5.  
  6. def lcg(modulus, a, c, seed):
  7.     """Linear congruential generator"""
  8.     while True:
  9.         seed = (a * seed + c) % modulus
  10.         yield seed / modulus
  11.  
  12. lcg_gen = lcg(2**32, 1664525, 1013904223, 0)
  13.  
  14.  
  15. n_bins = 50
  16. fig, axs = plt.subplots(2, 1, sharey=True, tight_layout=True)
  17. plt.ion()
  18. plt.show(block=False)
  19.  
  20.  
  21. ROOT = tk.Tk()
  22. ROOT.withdraw()
  23.  
  24. while True:
  25.  
  26.     USER_INP = tk.simpledialog.askstring(title="ГПСЧ", prompt="Длина: ")
  27.  
  28.  
  29.     if type(USER_INP) is not str:
  30.         plt.show(block=True)
  31.         break
  32.  
  33.     try:
  34.         n = int(USER_INP)
  35.         if n <= 0:
  36.             raise ValueError
  37.  
  38.     except ValueError:
  39.         tk.messagebox.showinfo("Ошибка!", "Неарвильный ввод. Необходимо целое число")
  40.         continue
  41.  
  42.  
  43.     x1 = [random.random() for i in range(n)]
  44.     x2 = [next(lcg_gen) for i in range(n)]
  45.  
  46.  
  47.     axs[0].clear()
  48.     axs[0].hist(x1, bins=n_bins)
  49.     axs[0].set_title('random() '+ str(n))
  50.      
  51.     axs[1].clear()
  52.     axs[1].hist(x2, bins=n_bins)
  53.     axs[1].set_title('lcg() '+ str(n))
  54.  
  55.  
  56.     plt.draw()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement