jotto

Untitled

Nov 14th, 2014
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.94 KB | None | 0 0
  1. from time import sleep
  2. from datetime import datetime
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5.  
  6.  
  7. def counted(fn):
  8.     """Function run counter"""
  9.  
  10.     def wrapper(*args, **kwargs):
  11.         wrapper.called += 1
  12.         return fn(*args, **kwargs)
  13.  
  14.     wrapper.called = 0
  15.     wrapper.__name__ = fn.__name__
  16.     return wrapper
  17.  
  18.  
  19. def menu():
  20.     """Generates menu"""
  21.     print("--------------------------------------------")
  22.     menu_input = input("Choose RNG (Random Number Generator):\n1. Mysterious method\n2. Timestamp method"
  23.                        "\n3. Create a histogram\n4. Quit program\n")
  24.     main(menu_input)
  25.  
  26.  
  27. def main(menu_input):
  28.     """Checks for answer"""
  29.     if menu_input == "1":
  30.         return random_four()
  31.     elif menu_input == "3":
  32.         gauss_plot()
  33.     elif menu_input == "2":
  34.         print("Your RGN is:\n%s" % (gauss_distribution()))
  35.         print_table()
  36.         return menu()
  37.     elif menu_input == "4":
  38.         return quit()
  39.     elif not menu_input:
  40.         print("You did not type anything.")
  41.         gauss_plot()
  42.         return menu()
  43.     else:
  44.         print("You typed wrong number.")
  45.         return menu()
  46.  
  47.  
  48. @counted
  49. def random_four():
  50.     """Creates mysterious RGN"""
  51.     if random_four.called == 1:
  52.         print("Your RGN is:\n4")
  53.     elif random_four.called == 2:
  54.         print("Seriously, your RGN is:\n4")
  55.     elif random_four.called == 3:
  56.         print("I mean it! Your RGN is:\n4")
  57.     elif random_four.called == 4:
  58.         print("Really! It is possible. Your RGN is:\n4")
  59.     else:
  60.         print("Your RGN is:\n4")
  61.     return menu()
  62.  
  63.  
  64. @counted
  65. def gauss_distribution():
  66.     """RGN with gauss distribution"""
  67.     now = datetime.now()
  68.     now_us = int(now.microsecond)
  69.     now_us_string = str(now_us)
  70.     now_us_string_length = len(now_us_string)
  71.     i = 0
  72.     h = 0
  73.     while i < now_us_string_length:
  74.         h += int(now_us_string[i])
  75.         i += 1
  76.     random = int((int(now_us / 10000) + h) * 99 / 153)
  77.     sleep(0.0314159265359 + (0.00314159265359 * (gauss_distribution.called % 2)) -
  78.           (0.00314159265359 * (gauss_distribution.called % 3) +
  79.            (0.00314159265359 * (gauss_distribution.called % 4) -
  80.             (0.00314159265359 * (gauss_distribution.called % 5)))))
  81.     return random
  82.  
  83.  
  84. def print_table():
  85.     """Prints a table of RGN"""
  86.     question = input("Would You like to print a table?\n")
  87.     if len(question) > 2 and is_number(question[1:2]) and question[0].lower() == "y":
  88.         for j in range(int(question[2])):
  89.             table = [gauss_distribution() for unused in range(int(question[1]))]
  90.             print(table)
  91.         return 0
  92.     elif question.lower() == "y" or question.lower() == "yes":
  93.         table_x = int(input("Provide x:\n"))
  94.         table_y = int(input("Provide y:\n"))
  95.         for j in range(table_y):
  96.             table = [gauss_distribution() for unused in range(table_x)]
  97.             print(table)
  98.         return 0
  99.     elif question.lower() == "n" or question.lower() == "no":
  100.         return 0
  101.     else:
  102.         print("Use only: Y, Yes, N, No, Yxy (where x and y are size of table)")
  103.         print_table()
  104.  
  105.  
  106. def is_number(n):
  107.     """Check if a char on String is a number"""
  108.     try:
  109.         int(n)
  110.         return True
  111.     except ValueError:
  112.         return False
  113.  
  114.  
  115. def gauss_plot():
  116.     """Creates histogram of density distribution"""
  117.     try:
  118.         cont = []
  119.         count = [0 for unused in range(100)]
  120.         for v in range(100):
  121.             cont.append(v)
  122.         n = int(input("Provide number of trials:\n"))
  123.         for g in range(n):
  124.             temp = gauss_distribution()
  125.             count[temp] += 1
  126.         pos = np.arange(len(cont))
  127.         width = 1.0
  128.  
  129.         ax = plt.axes()
  130.         ax.set_xticks(pos + width)
  131.  
  132.         plt.bar(pos, count, width, color='blue')
  133.         plt.show()
  134.         menu()
  135.     except ValueError:
  136.         print("Please type number.")
  137.         gauss_plot()
  138.  
  139.  
  140. menu()
Advertisement
Add Comment
Please, Sign In to add comment