# [SciPy-User] random points within an ellipse

David Goldsmith d.l.goldsmith@gmail....
Thu Aug 5 02:49:26 CDT 2010

```This is embarrassing: clearly, that was not a uniform distribution over the
ellipse; here's the fix:

def rand_in_ellipse(a, b=1, offset=0):
angle = 2*pi*random() - offset
r = np.sqrt( (a*np.cos(angle))**2 + (b*np.sin(angle))**2 )
r *= random()
x = r * np.cos(angle)
y = r * np.sin(angle)
return np.array((x,y))

Which yields the attached.  (It looks denser in the center because the
uniformity in the size of the points increases in significance as that size
becomes of the same order as the distance away from other points.)

DG
On Wed, Aug 4, 2010 at 11:54 PM, 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
>>
>>
>> On Wed, Aug 4, 2010 at 8:13 PM, Benjamin Root <ben.root@ou.edu> wrote:
>>
>>> On Wed, Aug 4, 2010 at 7:18 PM, alex <argriffi@ncsu.edu> wrote:
>>>
>>>> On Wed, Aug 4, 2010 at 7:38 PM, Benjamin Root <ben.root@ou.edu> wrote:
>>>>
>>>>> Hi,
>>>>>
>>>>> For a project, I need to create sets of random coordinates within a 2d
>>>>> domain.  To start, I have been creating random x and y coordinates, which
>>>>> has worked very nicely.  However, I would like to start doing some fancier
>>>>> domains like ellipses and crescents.  Does anybody know of any useful tricks
>>>>> for doing this?
>>>>>
>>>>> Thanks,
>>>>> Ben Root
>>>>>
>>>>
>>>> For an ellipse you could start with random points in a disk like this
>>>> http://mathworld.wolfram.com/DiskPointPicking.html
>>>> and then I think you can just stretch along an axis to give the points
>>>> inside the ellipse disk.
>>>> For fancier shapes you can do rejection sampling as long as you can get
>>>> a bounding box and
>>>> you can tell what is inside vs outside the domain.
>>>>
>>>> Alex
>>>>
>>>>
>>> Thanks Alex, that certainly does put me in the correct direction. It
>>> appears that there isn't an analogous expression for an ellipse as there was
>>> for a disc.  I probably will have to just take your suggestion and do some
>>> scaling/rotation upon the set of points to get what I want.  It should be
>>> more than sufficient, and it does look better than my original attempts.
>>>
>>> Ben Root
>>>
>>> _______________________________________________
>>> SciPy-User mailing list
>>> SciPy-User@scipy.org
>>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>>
>>>
>>
>>
>> --
>> Mathematician: noun, someone who disavows certainty when their uncertainty
>> set is non-empty, even if that set has measure zero.
>>
>> Hope: noun, that delusive spirit which escaped Pandora's jar and, with her
>> lies, prevents mankind from committing a general suicide.  (As interpreted
>> by Robert Graves)
>>
>
>
>
> --
> Mathematician: noun, someone who disavows certainty when their uncertainty
> set is non-empty, even if that set has measure zero.
>
> Hope: noun, that delusive spirit which escaped Pandora's jar and, with her
> lies, prevents mankind from committing a general suicide.  (As interpreted
> by Robert Graves)
>

--
Mathematician: noun, someone who disavows certainty when their uncertainty
set is non-empty, even if that set has measure zero.

Hope: noun, that delusive spirit which escaped Pandora's jar and, with her
lies, prevents mankind from committing a general suicide.  (As interpreted
by Robert Graves)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20100805/a93a1738/attachment-0001.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 4thirds_ellipse.png
Type: image/png
Size: 213362 bytes
Desc: not available
Url : http://mail.scipy.org/pipermail/scipy-user/attachments/20100805/a93a1738/attachment-0001.png
```