Story Details

  • Non-random uniform disk sampling

    Posted: 2025-01-27 17:09:20

    This post explores the problem of uniformly sampling points within a disk and reveals why a naive approach using polar coordinates leads to a concentration of points near the center. The author demonstrates that while generating a random angle and a random radius seems correct, it produces a non-uniform distribution due to the varying area of concentric rings within the disk. The solution presented involves generating a random angle and a radius proportional to the square root of a random number between 0 and 1. This adjustment accounts for the increasing area at larger radii, resulting in a truly uniform distribution of sampled points across the disk. The post includes clear visualizations and mathematical justifications to illustrate the problem and the effectiveness of the corrected sampling method.

    Summary of Comments ( 23 )
    https://news.ycombinator.com/item?id=42843252

    HN users discuss various aspects of uniformly sampling points within a disk. Several commenters point out the flaws in the naive sqrt(random()) approach, correctly identifying its tendency to cluster points towards the center. They offer alternative solutions, including the accepted approach of sampling an angle and radius separately, as well as using rejection sampling. One commenter explores generating points within a square and rejecting those outside the circle, questioning its efficiency compared to other methods. Another details the importance of this problem in ray tracing and game development. The discussion also delves into the mathematical underpinnings, with commenters explaining the need for the square root on the radius to achieve uniformity and the relationship to the area element in polar coordinates. The practicality and performance of different methods are a recurring theme, including comparisons to pre-calculated lookup tables.