Skip to content

Instantly share code, notes, and snippets.

@sshh12
Created November 5, 2017 23:11
Show Gist options
  • Save sshh12/6186968785dd6f7b4c9a3b34aee9ac76 to your computer and use it in GitHub Desktop.
Save sshh12/6186968785dd6f7b4c9a3b34aee9ac76 to your computer and use it in GitHub Desktop.
Prob of even distribution of n over k values.
import matplotlib.pyplot as plt
import random
import math
def get_prob(n, k, trials=200000):
perfect = 0
for _ in range(trials):
nums = [random.randint(0, n - 1) for _ in range(k)]
for i in range(n):
if nums.count(i) != k // n:
break
else:
perfect += 1
return perfect / trials
###### Testing #######
n_tests = [1, 2, 3, 4, 5, 6, 8, 9, 10]
k_coefs = [1, 2, 3, 4, 5, 6]
plt.title("Noushin's Probability Theorem")
plt.xlabel('n')
plt.ylabel('Prob of even distribution')
legend = []
for coef in k_coefs:
n_values = []
prob_values = []
for n in n_tests:
p = get_prob(n, n * coef)
print(n, ", ", coef, "n - ", p)
n_values.append(n)
prob_values.append(p)
plt.plot(n_values, prob_values)
legend.append("k=" + str(coef) + "n")
plt.legend(legend)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment