擅长:python、mysql、java
<p>使用<a href="http://docs.python.org/3/library/random.html#random.sample" rel="nofollow">^{<cd1>}</a>。它可以按任何顺序工作:</p>
<pre><code>>>> random.sample([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 4)
[4, 2, 9, 0]
>>> random.sample('even strings work', 4)
['n', 't', ' ', 'r']
</code></pre>
<p>与<code>random</code>模块中的所有函数一样,您可以像通常一样定义种子:</p>
<pre><code>>>> import random
>>> lst = list(range(10))
>>> random.seed('just some random seed') # set the seed
>>> random.sample(lst, 4)
[6, 7, 2, 1]
>>> random.sample(lst, 4)
[6, 3, 1, 0]
>>> random.seed('just some random seed') # use the same seed again
>>> random.sample(lst, 4)
[6, 7, 2, 1]
>>> random.sample(lst, 4)
[6, 3, 1, 0]
</code></pre>