Parallel random distribution
<p>I have two iterators in python and both should follow the same "random" distribution <strong>(both should run in parallel)</strong>. For instance:</p>

<pre><code>class Iter1(object):
   def __iter__(self):
       for i in random_generator():
          yield i

class Iter2(object):
   def __iter__(self):
       for i in random_generator():
          yield i

for el1, el2 in zip(Iter1(), Iter2()):
    print '{} {}'.format(el1, el2)
</code></pre>

<p>output should be somethig like:</p>

<pre><code>0.53534 0.53534
0.12312 0.12312
0.19238 0.19238
</code></pre>

<p>How can I define <code>random_generator()</code> in a way that it creates the same random distributions <strong>in parallel</strong> for both iterators. </p>

<p>Note:</p>

<ul>
<li>They should run in parallel</li>
<li>I can't generate the sequence in advance (it is a streaming, so I don't know the size of the sequence)</li>
</ul>

<p>Thanks.</p>

<p>Specify the same seed to each call of <code>random_generator</code>:</p>

<pre><code>import random

def random_generator(l, seed=None):
    r = random.Random(seed)
    for i in range(l):
        yield r.random()


class Iter1(object):
   def __init__(self, seed):
       self.seed = seed

   def __iter__(self):
       for i in random_generator(10, self.seed):
          yield i


class Iter2(object):
   def __init__(self, seed):
       self.seed = seed
   def __iter__(self):
       for i in random_generator(10, self.seed):
          yield i


# The seed can be any hashable object, but don't use None; that
# tells random.seed() to use the current time. But make sure that
# Python itself isn't using hash randomization.
common_seed = object()
for el1, el2 in zip(Iter1(common_seed), Iter2(common_seed)):
    print '{} {}'.format(el1, el2)
</code></pre>

python random iterator generator