Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Random Point from a 2D Donut Distribution

A donut distribution (for lack of a better term) is a random distribution of points in a 2-dimensional plane, forming a donut-like shape. The distribution is defined by two parameters: the radius r and spread s, in which the distance to the origin follows a normal (Gaussian) distribution around r, with a standard deviation s. The angular distribution is uniform in the range [0,2π).

The challenge

Given a radius r and spread s, your code should yield the Cartesian ((x,y)) coordinates of a single point chosen from this distribution.

Remarks

  • Running your code multiple times with the same input should result in the specified distribution.
  • Outputting polar coordinates is too trivial and not allowed.
  • You can output Cartesian coordinates in any way allowed by the default I/O rules.
    • This includes complex values.

Valid approaches

Several algorithms can be used to yield the desired distribution, including but not limited to

  1. Choose a from the uniform distribution [0,2π) and b from the normal distribution (r,s).
    Let x = b*cos(a) and y = b*sin(a).
  2. Choose a from the uniform distribution [0,4) and b from the normal distribution (r,s).
    Let x+y*i = b*i^a.
  3. Choose a,b,c all from the normal distribution (0,1).
    Let d = a+b*i and x+y*i = d/abs(d) * (c*s+r).

Example distributions (N=1000)

Below: r=1, s=0.1

r=1, s=0.1

Below: r=3, s=1

r=3, s=1

Below: r=1, s=0

r=1, s=0

Below: r=100, s=5

r=100, s=5

Answer*

Draft saved
Draft discarded
Cancel

AltStyle によって変換されたページ (->オリジナル) /