skip to content

When scattering dropped loot uniformly inside a circle around a defeated enemy, why must the random radius use a square root?

level: middleimportance: should knowfreq 30%

answer

  1. area grows faster than radius
  2. points crowd the middle
  3. invert the area fraction
  4. square root for a disk, cube root for a ball
  5. uniform height on a sphere

basics

~20 s

Area grows with the square of the radius, so a radius drawn as radius * u crowds points toward the centre. Drawing radius * sqrt(u) with an angle of 2 * pi * v makes every equal patch of the disk equally likely.

solid answer

~50 s

With `r = radius * u`, half of the points land within half the radius, but that inner disk is only a quarter of the area, so loot visibly clumps at the centre. The share of area inside radius `r` is `(r / radius)^2`; setting that equal to a uniform `u` and solving gives `r = radius * sqrt(u)`, with an independent angle `theta = 2 * pi * v`. The alternative is rejection sampling: draw `x, y` uniformly in the bounding square and keep the point if `x^2 + y^2 <= radius^2`, which accepts `pi / 4`, about 78.5 percent, so roughly 1.27 tries per point. On a sphere's surface, draw `z` uniformly in `[-1, 1]` and the angle uniformly, then use a ring radius of `sqrt(1 - z^2)`; uniform latitude and longitude crowds the poles. Inside a ball, the radius uses a cube root.

code

pseudocode · 10 lines
pseudocode
function point_in_disk(center, radius, rng):
    r = radius * sqrt(rng.float01())          // float01: uniform in [0, 1)
    theta = 2 * PI * rng.float01()
    return center + vec2(r * cos(theta), r * sin(theta))

function direction_on_sphere(rng):
    z = 1 - 2 * rng.float01()                 // uniform in (-1, 1]
    phi = 2 * PI * rng.float01()
    s = sqrt(max(0, 1 - z * z))
    return vec3(s * cos(phi), s * sin(phi), z)

go deeper

for a junior

Recall the fix and the reason in one line: area grows with the square of the radius, so the radius is the maximum radius times the square root of a uniform number.

for a middle

Derive the square root from the area fraction, offer rejection sampling with its acceptance rate, and give the uniform-height recipe for directions on a sphere.

for a senior

Spot clumping from a screenshot, choose between closed-form and rejection sampling by cost and draw-count stability, and extend the derivation to rings and balls.

for a principal

Provide shared, tested sampling helpers for disks, rings, spheres and balls, so features stop reinventing them and seeded runs consume a predictable number of draws.

## The bug: loot that clumps in the middle A common feature scatters dropped items, debris or spawn points **uniformly inside a circle** of some radius around a point. The first implementation usually draws an angle uniformly and a radius uniformly: `theta = 2 * pi * v`, `r = radius * u`, with `u` and `v` independent uniform numbers in `[0, 1)`. The result clumps visibly at the centre. The reason is geometric. A thin ring at distance `r` from the centre has an area proportional to its circumference, `2 * pi * r`. A uniform radius gives every ring the same number of points, so the small rings near the centre are packed densely and the large outer rings are sparse. Point density per unit area falls off as `1 / r`. Concretely, half of all points land within half the radius, yet that inner disk is only `(1/2)^2 = 1/4` of the area. ## Deriving the square root Uniform over the disk means the chance of landing in any region equals that region's share of the area. Work it out for the radius alone: 1. The share of the disk's area inside radius `r` is `(pi * r^2) / (pi * radius^2) = (r / radius)^2`. 2. A uniform radius sampler must therefore land inside `r` with exactly that chance. 3. Draw `u` uniformly in `[0, 1)` and set `(r / radius)^2 = u`. Since `u` is below any value `t` with chance `t`, the radius is below `r` with chance `(r / radius)^2`, as required. 4. Solve: `r = radius * sqrt(u)`. 5. Draw the angle independently and uniformly: `theta = 2 * pi * v`, then `x = r * cos(theta)`, `y = r * sin(theta)`. This is **inverse-transform sampling**: write down the fraction of the shape covered up to some parameter value, set it equal to a uniform number and solve for the parameter. The same move handles a **ring** (an annulus between an inner radius `r0` and an outer radius `r1`): the covered fraction is `(r^2 - r0^2) / (r1^2 - r0^2)`, so `r = sqrt(r0^2 + u * (r1^2 - r0^2))`. It also keeps a ring of spawn points from crowding the inner edge. ## Rejection sampling as the alternative **Rejection sampling** draws `x` and `y` uniformly in the square `[-radius, radius]` on both axes and keeps the point only if `x^2 + y^2 <= radius^2`, trying again otherwise. Points in the square are uniform, so the survivors are uniform in the disk. - The acceptance rate is the area ratio `pi / 4`, about **78.5 percent**, so the expected number of tries is `4 / pi`, about **1.27**. - It needs no square root, sine or cosine, which can make it cheaper per accepted point. - Its draw count varies, which matters for a seeded run: a stream that serves rejection loops consumes a variable number of values, so other systems should not share it. - In higher dimensions it degrades quickly, because a ball fills a smaller and smaller share of its bounding cube. ## Directions and points on a sphere Random directions (a spread of shrapnel, an explosion's debris, a random point on a planet) need **uniform points on a sphere's surface**. Several obvious recipes are wrong. | Recipe | Uniform? | Why | |---|---|---| | Latitude and longitude angles both uniform | No | Rings near the poles are small, so the same share of points crowds them | | Point uniform in the cube, then normalised | No | Directions toward the cube's corners get extra volume | | Point uniform in the cube, rejected outside the ball, then normalised | Yes | The ball is symmetric, so every direction is equally likely | | Height `z` uniform in `[-1, 1]`, angle uniform, ring radius `sqrt(1 - z^2)` | Yes | Equal slabs of height cut equal areas of a sphere | | Three independent normally distributed values, normalised | Yes | That joint distribution is the same in every direction | The height recipe rests on **Archimedes' hat-box theorem**: the area of a sphere's slice between two parallel planes depends only on the distance between them. A uniform height therefore gives uniform area. The point is `(s * cos(phi), s * sin(phi), z)` with `s = sqrt(1 - z^2)` and `phi = 2 * pi * v`. ## Inside a ball For points **inside** a ball, volume up to radius `r` grows as `r^3`, so the same derivation gives `r = radius * cbrt(u)` with a uniform direction from the sphere recipe. The pattern generalises: in `d` dimensions the radius uses the `d`-th root. Whichever method you use, draw from the generator stream that owns the feature, so that a seeded replay scatters the same loot in the same places.

  • How do you scatter points uniformly inside a ring between an inner and an outer radius?
    Apply the same area argument to the ring. The share of its area inside radius `r` is `(r^2 - r0^2) / (r1^2 - r0^2)`; set that equal to a uniform `u` and solve: `r = sqrt(r0^2 + u * (r1^2 - r0^2))`, with a uniform angle. A uniform radius between `r0` and `r1` would crowd the inner edge.
  • How do you pick a uniform random point inside a sphere, not on its surface?
    Volume inside radius `r` grows as `r^3`, so use `r = radius * cbrt(u)` together with a uniform direction from the height recipe. Alternatively, draw a point in the bounding cube and reject it if it lies outside the ball; acceptance is `pi / 6`, about 52 percent.

  • A uniform radius and a uniform angle give a uniform point in a disk.
  • Uniform latitude and longitude angles give uniform points on a sphere.
  • Normalising a point drawn uniformly in a cube gives a uniform direction.
  • Rejection sampling a disk wastes about half of its candidate points.
  • The square-root trick for a disk also works for points inside a ball.