【发布时间】:2015-03-29 07:22:39
【问题描述】:
我的程序生成了可以称为 switch(x) 的序列。
下面是生成它们的算法的描述:
# define a switch(x) sequence of length n^2
def switch(x):
s = [] # define a bit string s:
s.append(random.choice([True, False])) # set its first bit randomly,
for i in range(1, n * n): # but for every subsequent bit i,
prior = s[i-1] # find its prior bit i-1,
shouldSwitch = x > random.random() # and with probability 1-x,
if shouldSwitch: s.append(not prior) # set i to opposite of its prior;
else: s.append(prior) # otherwise, have i = its prior.
return s
在 x = .5 时,该序列被认为是完全随机的位序列。偏离 x 通过产生过于频繁地交替或重复位的序列来扭曲这种随机性。
我编写了一个程序,计算生成的 switch(x) 序列的平均交替率。
r = 0.0
for i in range(len(s)-1):
if s[i] != s[i+1]:
r = r + 1
rate = r/(len(s)-1)
当然,当我通过 switch(x) 序列时,无论 x 是什么,我总是获得一个相对接近 x 的速率。比如,在 1/100 以内。
但假设我像这样转换生成的 switch(x) 序列(其中 len(s) = n*n):
s1 = switch(x)
s2 = []
for i in range(n):
for j in range(n):
s2.append(s1[i + j*n])
每当我计算以这种方式转换的序列的交替率时,我总是得到一个非常接近 0.5 的值!一致性是可怕的。
这对我来说没有多大意义,尤其是对于非常接近 0 或 1 的 x 值。所以我希望你能帮助我找出问题所在。
抱歉,如果我的风格/效率有问题。
【问题讨论】: