jotto

PRNG gauss + uniform

Nov 4th, 2014
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.83 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 uniform_distribution():
  66.     """RGN with uniform 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.     random = int(now_us/10000)
  78.     sleep(0.0314159265359 + (0.00314159265359 * (uniform_distribution.called % 2)) -
  79.           (0.00314159265359 * (uniform_distribution.called % 3) +
  80.            (0.00314159265359 * (uniform_distribution.called % 4) -
  81.             (0.00314159265359 * (uniform_distribution.called % 5)))))
  82.     return random
  83.  
  84.  
  85. @counted
  86. def gauss_distribution():
  87.     """RGN with Gauss distribution"""
  88.     now = datetime.now()
  89.     now_us = int(now.microsecond)
  90.     now_us_string = str(now_us)
  91.     now_us_string_length = len(now_us_string)
  92.     h = 0
  93.     for i in range(now_us_string_length):
  94.         h += int(now_us_string[i])
  95.         h += 2*is_number(now_us_string[i])
  96.     h *= 3/2
  97.     random = int(int(now_us/10000 + h)/2)
  98.     sleep(0.0314159265359 + (0.00314159265359 * is_number(now_us_string[0]) * 0.1 * (gauss_distribution.called % 2)) -
  99.           (0.00314159265359 * is_number(now_us_string[1]) * 0.1 * (gauss_distribution.called % 3) +
  100.            (0.00314159265359 * is_number(now_us_string[2]) * 0.1 * (gauss_distribution.called % 4) -
  101.             (0.00314159265359 * is_number(now_us_string[3]) * 0.1 * (gauss_distribution.called % 5)))))
  102.     return random
  103.  
  104.  
  105. def print_table():
  106.     """Prints a table of RGN"""
  107.     question = input("Would You like to print a table?\n")
  108.     if len(question) > 2 and is_number(question[1:2]) and question[0].lower() == "y":
  109.         for j in range(int(question[2])):
  110.             table = [uniform_distribution() for unused in range(int(question[1]))]
  111.             print(table)
  112.         return 0
  113.     elif question.lower() == "y" or question.lower() == "yes":
  114.         table_x = int(input("Provide x:\n"))
  115.         table_y = int(input("Provide y:\n"))
  116.         for j in range(table_y):
  117.             table = [uniform_distribution() for unused in range(table_x)]
  118.             print(table)
  119.         return 0
  120.     elif question.lower() == "n" or question.lower() == "no":
  121.         return 0
  122.     else:
  123.         print("Use only: Y, Yes, N, No, Yxy (where x and y are size of table)")
  124.         print_table()
  125.  
  126.  
  127. def is_number(n):
  128.     """Check if a char on String is a number"""
  129.     try:
  130.         int(n)
  131.         return True
  132.     except ValueError:
  133.         return False
  134.  
  135.  
  136. def gauss_plot():
  137.     """Creates histogram of density distribution"""
  138.     try:
  139.         cont = []
  140.         count = [0 for unused in range(100)]
  141.         for v in range(100):
  142.             cont.append(v)
  143.         n = int(input("Provide number of trials:\n"))
  144.         for g in range(n):
  145.             temp = gauss_distribution()
  146.             count[temp] += 1
  147.         pos = np.arange(len(cont))
  148.         width = 1.0
  149.  
  150.         ax = plt.axes()
  151.         ax.set_xticks(pos + width)
  152.  
  153.         plt.bar(pos, count, width, color='blue')
  154.         plt.show()
  155.         menu()
  156.     except ValueError:
  157.         print("Please type number.")
  158.         gauss_plot()
  159.  
  160.  
  161. menu()
Advertisement
Add Comment
Please, Sign In to add comment