Skip to content

Instantly share code, notes, and snippets.

@Nydhal
Last active April 26, 2018 04:55
Show Gist options
  • Select an option

  • Save Nydhal/54c61103899d0eddad3a02281f344405 to your computer and use it in GitHub Desktop.

Select an option

Save Nydhal/54c61103899d0eddad3a02281f344405 to your computer and use it in GitHub Desktop.
#Python code to sample n trials given N and M (short but not best way) https://twitter.com/CutTheKnotMath/status/989147356129058816
import random as r,itertools as t
def s(M,N,L=[0]):
while len(L)<M+1:
x=r.randint(1,N)
if x!=L[-1]:L.append(x)
return len([x[0] for x in t.groupby([(L[i]>L[i-1]) for i in range(2,M+1)])])
M,N,n = 100,100,100000
l = []
for i in range(1,n):
l +=[s(M,N)]
print("M="+str(M)+", N="+str(N)+", n="+str(n))
print("Computational limit: "+ str(sum(l) / float(len(l))) )
print("Analytical limit: "+ str(1+(M-2)*((2*N-1)/(3*N-3))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment