[SciPy-User] random points within an ellipse
Benjamin Root
ben.root@ou....
Thu Aug 5 10:06:00 CDT 2010
On Thu, Aug 5, 2010 at 1:54 AM, David Goldsmith <d.l.goldsmith@gmail.com>wrote:
> Whoops, sorry, in the example, the call to rand_in_ellipse should have 4./3
> as a single argument, i.e., the line should look like
>
> sample = np.array([rand_in_ellipse(4./3) for i in np.arange(10000)])
>
>
> DG
>
> On Wed, Aug 4, 2010 at 11:50 PM, David Goldsmith <d.l.goldsmith@gmail.com>wrote:
>
>> For the ellipse:
>>
>> import numpy as np
>> from numpy.random import random_sample as random
>>
>> pi = np.pi
>> def rand_in_ellipse(a, b=1, offset=0):
>> angle = 2*pi*random() - offset
>> x = a * random() * np.cos(angle)
>> y = b * random() * np.sin(angle)
>> return np.array((x,y))
>>
>> import matplotlib.pyplot as plt
>>
>> fig = plt.figure()
>>
>>
>> ax = fig.add_subplot(111)
>>
>> sample = np.array([rand_in_ellipse() for i in np.arange(10000)])
>> ax.scatter(sample[:,0], sample[:,1])
>>
>>
>> plt.show() # Figure attached
>>
>> DG
>>
>>
Unfortunately, that does not produce a uniform sample. It is heavily
biased along the major and minor axes.
Ben Root
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20100805/02842446/attachment.html
More information about the SciPy-User
mailing list