[SciPy-User] random points within an ellipse
Tue Aug 10 11:11:07 CDT 2010
Excerpts from nicky van foreest's message of Sun Aug 08 17:03:07 -0400 2010:
> Good idea to include the graphical result.
> I must admit that the results of the latest mail by David do not
> satisfy my "random intuition". There is a thick cloud around the
> origin, which seems unnatural to me.
> Just for fun I implemented a rejection method that seems to give good
> results. From an algorithmic perspective my implementation is far from
> optimal (BTW, I welcome feedback on how to speed up code like this.)
> However, the rejection method does not seem that bad, roughly 20% of
> the numbers is rejected, and there is no need to compute cosines and
> the like. The resulting graph seems ok to me.
> I modified the example of DG somewhat for the example below.
I think this version (from my original post) is fairly optimal:
def get_random_ellipse(n, x0, y0):
xout = numpy.zeros(n)
yout = numpy.zeros(n)
while nkeep < n:
x=2*x0*(random.random(n-nkeep) - 0.5)
y=2*y0*(random.random(n-nkeep) - 0.5)
w,=where( ( (x/x0)**2 + (y/y0)**2 ) < 1 )
if w.size > 0:
xout[nkeep:nkeep+w.size] = x[w]
yout[nkeep:nkeep+w.size] = y[w]
nkeep += w.size
More information about the SciPy-User