How to generate a random UUID which is reproducible (with a seed) in Python
<p>The <a href="https://docs.python.org/2/library/uuid.html#uuid.uuid4" rel="noreferrer">uuid4()</a> function of Python's module <code>uuid</code> generates a random UUID, and seems to generate a different one every time:</p>
<pre><code>In [1]: import uuid

In [2]: uuid.uuid4()
Out[2]: UUID('f6c9ad6c-eea0-4049-a7c5-56253bc3e9c0')

In [3]: uuid.uuid4()
Out[3]: UUID('2fc1b6f9-9052-4564-9be0-777e790af58f')
</code></pre>
<p>I would like to be able to generate the same random UUID every time I run a script - that is, I'd like to seed the random generator in <code>uuid4()</code>. Is there a way to do this? (Or achieve this by some other means)?</p>
<h3>What I've tried so far</h3>
<p>I've to generate a UUID using the <code>uuid.UUID()</code> method with a random 128-bit integer (from a seeded instance of <code>random.Random()</code>) as input:</p>
<pre><code>import uuid
import random

rd = random.Random()
rd.seed(0)
uuid.UUID(rd.getrandbits(128))
</code></pre>
<p>However, <code>UUID()</code> seems not to accept this as input:</p>
<pre><code>Traceback (most recent call last):
  File &quot;uuid_gen_seed.py&quot;, line 6, in &lt;module&gt;
    uuid.UUID(rd.getrandbits(128))
  File &quot;/usr/lib/python2.7/uuid.py&quot;, line 133, in __init__
    hex = hex.replace('urn:', '').replace('uuid:', '')
AttributeError: 'long' object has no attribute 'replace'
</code></pre>
<p>Any other suggestions?</p>