vendredi 25 mars 2016

Numpy: Generate 2 sequences of n random numbers with no overlaps

For n = 5, two random sequences of integers in [0, 2] might look like:

 l1 = [2, 0, 2, 2, 0]
 l2 = [0, 1, 2, 2, 2]

I want to make sure that l1[i] != l2[i] for all i. My current method (looping over all values and replacing overlaps) seems inefficient:

n = 5
sample = np.arange(3)
l1 = np.random.choice(sample, size=n)
l2 = np.random.choice(sample, size=n)
mask = np.ones(sample.size, dtype=bool)
for i in xrange(n):
    if l1[i] == l2[i]:
        mask[l1[i]] = False
        l2[i] = np.random.choice(sample[mask])
        mask[l1[i]] = True

Is there a better way to do this?




Aucun commentaire:

Enregistrer un commentaire