Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import randrange
- from matplotlib import pyplot as plt
- # Auxiliary Functions
- # 1. Find unique digits of a number
- def unique(number):
- a = str(number)
- b = [int(element) for element in a]
- digits = sorted(b)
- un = list()
- for digit in digits:
- if digit not in un:
- un.append(digit)
- return len(un)
- # 2. Simulate for different numbers with fixed length = num of digits
- def simulate_fixed_length(sims, num_digits):
- # This list will be a list with len = 10.
- # Element in index 0 is the total number of cases our randomly created
- # numbers had only 1 digit, index 2 for a 2-unique-digit number and
- # index 9 if all the digits are different (10).
- counter = [0 for i in range(10)]
- start = 10 ** (num_digits - 1)
- stop = 10 ** num_digits
- for i in range(sims):
- r = randrange(start, stop)
- UN = unique(r)
- counter[UN - 1] += 1
- # if UN == 1:
- # print(r)
- return counter
- # 3. Simulate for different lengths
- def simulate(sims, num_digits_list):
- counters = list()
- for num_digit in num_digits_list:
- print("******************************************************")
- print("For " + str(num_digit) + "-digit numbers:")
- counter = simulate_fixed_length(sims, num_digit)
- counters.append(counter)
- print(counter)
- print()
- return counters
- # MAIN FUNCTION
- sims = 10**5
- num_digits_list = range(2, 15)
- counters = simulate(sims, num_digits_list)
- # Plotting
- x = range(1, 11)
- plt.figure()
- for i in range(len(counters)):
- y = counters[i]
- print(y)
- plt.plot(x, y)
- plt.title("Simulations = " + str(sims))
- titles = [str(i) + "-digit numbers" for i in num_digits_list]
- plt.legend(titles)
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement