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

时间:2014-09-13 22:17:43

标签: python string random

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

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

2 个答案:

答案 0 :(得分:4)

您可以使用random.seed。来自doc

  

初始化基本随机数生成器。

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

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

答案 1 :(得分:-2)

这是我的第一篇文章,所以请耐心等待。

您可以使用randint,删除和追加

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 

这应该可以完成工作,即使可能有更有效的方法。我希望这可以帮助您找到合适的代码来完成工作。祝你好运!