擅长:python、mysql、java
<p>正如<a href="http://docs.python.org/2/library/random.html#module-random" rel="noreferrer">the documentation</a>所解释的:</p>
<blockquote>
<p>The functions supplied by this module are actually bound methods of a hidden instance of the random.Random class. You can instantiate your own instances of Random to get generators that don’t share state.</p>
</blockquote>
<p>因此,您只需创建自己的<code>random.Random</code>实例,并使用它自己的种子,这根本不会影响全局函数:</p>
<pre><code>>>> import random
>>> x = [1, 2, 3, 4, 5, 6]
>>> random.Random(4).shuffle(x)
>>> x
[4, 6, 5, 1, 3, 2]
>>> x = [1, 2, 3, 4, 5, 6]
>>> random.Random(4).shuffle(x)
>>> x
[4, 6, 5, 1, 3, 2]
</code></pre>
<p>(您也可以在<code>Random</code>实例周围保留并重新创建<code>seed</code>实例,而不是一遍又一遍地创建新的实例;没有太大的区别。)</p>