random.random()返回范围为[0,1)的 均匀分布的 伪随机浮点数。该数字小于
p[0,1)范围内给定概率的给定数字
p。从而:
def flip(p): return 'H' if random.random() < p else 'T'
一些实验:
>>> N = 100>>> flips = [flip(0.2) for i in xrange(N)]>>> float(flips.count('H'))/N0.17999999999999999 # Approximately 20% of the coins are heads>>> N = 10000>>> flips = [flip(0.2) for i in xrange(N)]>>> float(flips.count('H'))/N0.20549999999999999 # Better approximation
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)