Sin*_*bar -3 python string random
是否有类似于 Python 中用于字符串的随机种子数生成器的东西?
假设我想从字符串中选择一个随机的字符序列'abcdefg'- 如果我使用,''.join(random.choice('abcdefg'))我每次都会得到不同的结果。我如何从这些字符中获取随机的字符串种子序列,类似于random.seed()Python中的函数?
您可以使用 random.seed。从文档
初始化基本随机数生成器。
因此,每个使用 random 的函数都使用种子,甚至选择:
>>> random.seed(1)
>>> random.choice('abcdef')
'a'
>>> random.choice('abcdef')
'f'
>>> random.choice('abcdef')
'e'
>>> random.seed(1)
>>> random.choice('abcdef')
'a'
>>> random.choice('abcdef')
'f'
>>> random.choice('abcdef')
'e'
Run Code Online (Sandbox Code Playgroud)