dimanche 8 mai 2016

Pythonic way to generate random uniformly distributed points withing hollow square lamina

Suppose we have a hollow square lamina of size 3. That is, we have a 3x3 square from whose center a unit square has been removed. I want to calculate an average of distances between 2 random, uniformly distributed points within such hollow square lamina. In other words, all the points, such that their

1<x<2 and 1<y<2

I wrote this code for numpy, but it has at least 2 problems: I have to throw away approximately 1/9 of all generated points and removing the numpy.array elements requires lots of RAM:

    x,y = 3*np.random.random((2,size,2))
x = x[
    np.logical_not(np.logical_and(
        np.logical_and(x[:,0] > 1, x[:,0] < 2),
        np.logical_and(x[:,1] > 1, x[:,1] < 2)
        ))
    ]
y = y[
    np.logical_not(np.logical_and(
        np.logical_and(y[:,0] > 1, y[:,0] < 2),
        np.logical_and(y[:,1] > 1, y[:,1] < 2)
        ))
    ]
n = min(x.shape[0], y.shape[0])

Is there an elegant way to generate those points right away, without removing the unfit ones?




Aucun commentaire:

Enregistrer un commentaire