python – 使用概率表生成长度为K的N“随机”字符串
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 使用概率表生成长度为K的N“随机”字符串,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1971字,纯文字阅读大概需要3分钟。
内容图文
![python – 使用概率表生成长度为K的N“随机”字符串](/upload/InfoBanner/zyjiaocheng/733/3c797fae358f4494a0d1d21a76c7ded2.jpg)
如何使用概率表创建长度为K的N“随机”字符串? K会是偶数.
prob_table = {'aa': 0.2, 'ab': 0.3, 'ac': 0.5}
假设K = 6,’acacab’的可能性高于’aaaaaa’.
这是我用于基于概率表生成合成序列的更大问题的子问题.我不确定如何使用概率表生成“随机”字符串?
到目前为止我所拥有的:
def seq_prob(fprob_table,K= 6, N= 10):
#fprob_table is the probability dictionary that you input
#K is the length of the sequence
#N is the amount of sequences
seq_list = []
#possibly using itertools or random to generate the semi-"random" strings based on the probabilities
return seq_list
解决方法:
有一些很好的方法来制作at the end of the documentation for the builtin random module描述的加权随机选择:
A common task is to make a random.choice() with weighted probabilities.
If the weights are small integer ratios, a simple technique is to build a sample population with repeats:
>>> weighted_choices = [('Red', 3), ('Blue', 2), ('Yellow', 1), ('Green', 4)]
>>> population = [val for val, cnt in weighted_choices for i in range(cnt)]
>>> random.choice(population)
'Green'
A more general approach is to arrange the weights in a cumulative distribution with itertools.accumulate(), and then locate the random value with bisect.bisect():
>>> choices, weights = zip(*weighted_choices)
>>> cumdist = list(itertools.accumulate(weights))
>>> x = random.random() * cumdist[-1]
>>> choices[bisect.bisect(cumdist, x)]
'Blue'
为了使后一种方法适应您的具体问题,我会这样做:
import random
import itertools
import bisect
def seq_prob(fprob_table, K=6, N=10):
choices, weights = fprob_table.items()
cumdist = list(itertools.accumulate(weights))
results = []
for _ in range(N):
s = ""
while len(s) < K:
x = random.random() * cumdist[-1]
s += choices[bisect.bisect(cumdist, x)]
results.append(s)
return results
这假设概率表中的关键字符串长度相同如果它们有多个不同的长度,则此代码有时(可能大部分时间!)给出长于K个字符的答案.我想它也假设K是密钥长度的精确倍数,但如果不是真的那么它实际上会起作用(它只会给出比K字符都长的结果字符串,因为没有办法准确地得到K).
内容总结
以上是互联网集市为您收集整理的python – 使用概率表生成长度为K的N“随机”字符串全部内容,希望文章能够帮你解决python – 使用概率表生成长度为K的N“随机”字符串所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。