Python中是否有字符串的随机种子

2024-05-19 09:33:12 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有类似于Python中字符串的随机种子数生成器?在

假设我想从字符串'abcdefg'中选择一个随机字符序列—如果我使用''.join(random.choice('abcdefg')),每次都会得到不同的结果。如何从这些字符中获取字符串的随机种子序列,类似于Python中random.seed()函数的随机种子序列?在


Tags: 函数字符串序列random字符种子seedjoin
2条回答

这是我的第一篇博文,所以请谨慎对待。在

你可以使用randint,remove和append

myString = 'abcdefg'

1. create an array numBank with length 0->6 (the length of string 'abcdefg')
2. use randint to get a random int in numBank
3. use that random integer as an index for myString[i] and append myString[i] to a new array
4. use remove.numBank(i) to delete the number from the numBank
5. use randint to get another random number from numBank
6. repeat steps 3-5 until numBank is empty 

即使可能有更有效的方法来完成这项工作,这也应该能完成。我希望这能帮助你想出正确的代码来完成任务。祝你好运!在

你可以用随机.seed. 从doc

Initialize the basic random number generator.

所以每个随机函数都使用种子,甚至选择:

>>> 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'

相关问题 更多 >

    热门问题