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>There is no way to control the random generation number in this way. If you want to do that you should create your own random function. But as another pythonic and simpler way you can just create one object and use <code>itertools.tee</code> in order to copy your iterator object to having the same result for your random sequences:</p>

<pre><code>In [28]: class Iter1(object):
             def __init__(self, number): 
                 self.number = number
             def __iter__(self):
                 for _ in range(self.number):
                   yield random.random()
   ....:                 

In [29]: 

In [29]: num = Iter1(5)

In [30]: from itertools import tee

In [31]: num, num2 = tee(num)

In [32]: list(zip(num, num2))
Out[32]: 
[(0.485400998727448, 0.485400998727448),
 (0.8801649381536764, 0.8801649381536764),
 (0.9684025615967844, 0.9684025615967844),
 (0.9980073706742334, 0.9980073706742334),
 (0.1963579685642387, 0.1963579685642387)]
</code></pre>

python random iterator generator