看起来你的代码后面有问题...(例如在收集统计信息时)。检查这个较小的片段:
import random, collections
data = collections.defaultdict(int)
for x in range(1000000):
data[round(random.random(),1)] += 1
print(data)
0
和1
的样本数量大约只有其他值的一半,而其他值则几乎是均匀分布的。defaultdict(<class 'int'>,
{0.4: 100083,
0.9: 99857,
0.3: 99892,
0.8: 99586,
0.5: 100108,
1.0: 49874, # Correctly about half the others
0.7: 100236,
0.2: 99847,
0.1: 100251,
0.6: 100058,
0.0: 50208}) # Correctly about half the others
试一下这个
positions = []
for dummy_i in range(10):
positions.append(random.randint (0, 10) / 10)
我认为在这里使用Numpy可能会更加简洁高效:
import numpy as np
positions = np.random.random(10000)
positions = np.round(positions, decimals=3)
collections.Counter
:尝试运行collections.Counter(round(random.random(), 1) for _ in range(10**6))
,你会发现对于0.1
到0.9
的计数大致相等。 - Mark Dickinson