Skip to content

Instantly share code, notes, and snippets.

@CuGBabyBeaR
Last active August 29, 2015 14:16
Show Gist options
  • Save CuGBabyBeaR/2f3f0394a83114ad5666 to your computer and use it in GitHub Desktop.
Save CuGBabyBeaR/2f3f0394a83114ad5666 to your computer and use it in GitHub Desktop.
假设我有一个单位长度的线段,我往上随机丢n-1个点,分成n段。应该怎么计算【最长的】那一段的期望长度?
"""
CuGBabyBeaR @ 2015-2-25
result:
2 0.751132
3 0.611519
4 0.520260
5 0.457999
6 0.407207
7 0.371987
8 0.338186
9 0.314609
10 0.293323
11 0.273639
12 0.256827
dtype: float64
[Finished in 1.1s]
"""
import random as r
import pandas as pd
import numpy as np
def longest(n):
node = []
for i in xrange(n-1):
node.append(r.random())
node.sort()
longest = 0
last = 0
for x in node:
if longest < x-last:
longest = x-last
last = x
pass
if longest < 1-last:
longest = 1-last
return longest
def dataset(n, count = 10000):
res = []
for x in xrange(count):
res.append(longest(n))
return res
def ana(frm = 2, to = 12, count = 10000):
d = {};
index = range(10000)
for x in xrange(frm, to + 1):
d[x] = pd.Series(dataset(x, count), index = index)
pass
df = pd.DataFrame(d)
print df.mean()
ana()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment