我想模拟一条消息的故障(例如:1000010011 => 1010000011)。有没有办法在 Python 中实现它?我尝试了以下方法,它有效:
import random
a = "1011101101"
b = [el for el in a] # b = ['1', '0', '1', '1', '1', '0', '1', '1', '0', '1']
random.shuffle(b)
print b # b = ['0', '1', '1', '1', '0', '1', '1', '1', '1', '0']
random.shuffle(b, random.random)
print b # b = ['1', '1', '0', '1', '1', '0', '1', '0', '1', '1']
我希望我的重新排序是正常/高斯分布的。例如:
import random
a = "1011101101"
b = [el for el in a] # b = ['1', '0', '1', '1', '1', '0', '1', '1', '0', '1']
random.shuffle(b,random.gauss(0.5,0.1))
print b # b = ['1', '0', '1', '1', '0', '0', '1', '1', '1', '1'] <= assume this is Gaussian distributed...
# OR
c = random.gauss(0.5,0.1)
random.shuffle(b,c)
print b # b = ['0', '0', '1', '1', '1', '0', '1', '1', '1', '1'] <= assume this is also Gaussian distributed...
但是,这不起作用,我收到消息:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\random.py", line 287, in shuffle
j = int(random() * (i+1))
TypeError: 'float' object is not callable
任何建议/评论将不胜感激。
谢谢
注意:我在这里只要求重新订购错误(例如:1000010011 => 1010000011)。但是,我还计划模拟突发错误(例如:1000010011 => 1011111011)、单个事件(例如:1000010011 => 1000010011)等。
其他相关问题:Python:二进制字符串错误模拟