Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from time import sleep
- from datetime import datetime
- import numpy as np
- import matplotlib.pyplot as plt
- def counted(fn):
- """Function run counter"""
- def wrapper(*args, **kwargs):
- wrapper.called += 1
- return fn(*args, **kwargs)
- wrapper.called = 0
- wrapper.__name__ = fn.__name__
- return wrapper
- def menu():
- """Generates menu"""
- print("--------------------------------------------")
- menu_input = input("Choose RNG (Random Number Generator):\n1. Mysterious method\n2. Timestamp method"
- "\n3. Create a histogram\n4. Quit program\n")
- main(menu_input)
- def main(menu_input):
- """Checks for answer"""
- if menu_input == "1":
- return random_four()
- elif menu_input == "3":
- gauss_plot()
- elif menu_input == "2":
- print("Your RGN is:\n%s" % (gauss_distribution()))
- print_table()
- return menu()
- elif menu_input == "4":
- return quit()
- elif not menu_input:
- print("You did not type anything.")
- gauss_plot()
- return menu()
- else:
- print("You typed wrong number.")
- return menu()
- @counted
- def random_four():
- """Creates mysterious RGN"""
- if random_four.called == 1:
- print("Your RGN is:\n4")
- elif random_four.called == 2:
- print("Seriously, your RGN is:\n4")
- elif random_four.called == 3:
- print("I mean it! Your RGN is:\n4")
- elif random_four.called == 4:
- print("Really! It is possible. Your RGN is:\n4")
- else:
- print("Your RGN is:\n4")
- return menu()
- @counted
- def uniform_distribution():
- """RGN with uniform distribution"""
- now = datetime.now()
- now_us = int(now.microsecond)
- now_us_string = str(now_us)
- now_us_string_length = len(now_us_string)
- i = 0
- h = 0
- while i < now_us_string_length:
- h += int(now_us_string[i])
- i += 1
- # random = int((int(now_us / 10000) + h) * 99 / 153)
- random = int(now_us/10000)
- sleep(0.0314159265359 + (0.00314159265359 * (uniform_distribution.called % 2)) -
- (0.00314159265359 * (uniform_distribution.called % 3) +
- (0.00314159265359 * (uniform_distribution.called % 4) -
- (0.00314159265359 * (uniform_distribution.called % 5)))))
- return random
- @counted
- def gauss_distribution():
- """RGN with Gauss distribution"""
- now = datetime.now()
- now_us = int(now.microsecond)
- now_us_string = str(now_us)
- now_us_string_length = len(now_us_string)
- h = 0
- for i in range(now_us_string_length):
- h += int(now_us_string[i])
- h += 2*is_number(now_us_string[i])
- h *= 3/2
- random = int(int(now_us/10000 + h)/2)
- sleep(0.0314159265359 + (0.00314159265359 * is_number(now_us_string[0]) * 0.1 * (gauss_distribution.called % 2)) -
- (0.00314159265359 * is_number(now_us_string[1]) * 0.1 * (gauss_distribution.called % 3) +
- (0.00314159265359 * is_number(now_us_string[2]) * 0.1 * (gauss_distribution.called % 4) -
- (0.00314159265359 * is_number(now_us_string[3]) * 0.1 * (gauss_distribution.called % 5)))))
- return random
- def print_table():
- """Prints a table of RGN"""
- question = input("Would You like to print a table?\n")
- if len(question) > 2 and is_number(question[1:2]) and question[0].lower() == "y":
- for j in range(int(question[2])):
- table = [uniform_distribution() for unused in range(int(question[1]))]
- print(table)
- return 0
- elif question.lower() == "y" or question.lower() == "yes":
- table_x = int(input("Provide x:\n"))
- table_y = int(input("Provide y:\n"))
- for j in range(table_y):
- table = [uniform_distribution() for unused in range(table_x)]
- print(table)
- return 0
- elif question.lower() == "n" or question.lower() == "no":
- return 0
- else:
- print("Use only: Y, Yes, N, No, Yxy (where x and y are size of table)")
- print_table()
- def is_number(n):
- """Check if a char on String is a number"""
- try:
- int(n)
- return True
- except ValueError:
- return False
- def gauss_plot():
- """Creates histogram of density distribution"""
- try:
- cont = []
- count = [0 for unused in range(100)]
- for v in range(100):
- cont.append(v)
- n = int(input("Provide number of trials:\n"))
- for g in range(n):
- temp = gauss_distribution()
- count[temp] += 1
- pos = np.arange(len(cont))
- width = 1.0
- ax = plt.axes()
- ax.set_xticks(pos + width)
- plt.bar(pos, count, width, color='blue')
- plt.show()
- menu()
- except ValueError:
- print("Please type number.")
- gauss_plot()
- menu()
Advertisement
Add Comment
Please, Sign In to add comment