This project compares four different methods for generating uniformly distributed random points within a unit circle. Each method is implemented in Python, visualized, and analyzed for performance.
The BEST Way to Find a Random Point in a Circle YouTube found at https://www.youtube.com/watch?v=4y_nmpv-9lI
YouTube Sources used:
- https://stackoverflow.com/questions/5837572/generate-a-random-point-within-a-circle-uniformly
- https://onlinelibrary.wiley.com/doi/10.1155/2017/3571419#derivation-of-the-irwin-hall-distribution
- https://programming.guide/random-point-within-circle.html#google_vignette
YouTube Chapters:
- Introduction
- Rejection Sampling
- Coordinate Systems
- Inverse Transform Sampling
- Infinite Triangle Sampling
- random() + random() vs random() * 2
- Irwin-Hall Distribution
- max(random(), random())
- Conclusion
How it works:
- Generate random (x, y) coordinates in the range [-1, 1].
- Keep the point if x^2 + y^2 <= 1, otherwise reject and try again.
Pros: Simple to implement and understand. Cons: Can be inefficient, especially for higher dimensions, as it discards points.
How it works:
- Generate two uniform random numbers u1 and u2 in [0, 1].
- Set radius r = max(u1, u2).
- Generate a random angle θ in [0, 2π].
- Convert to Cartesian coordinates: (r * cos(θ), r * sin(θ)).
Pros: Efficient and doesn't require rejection. Cons: May be less intuitive than other methods.
How it works:
- Generate a uniform random number u in [0, 1].
- Set radius r = sqrt(u).
- Generate a random angle θ in [0, 2π].
- Convert to Cartesian coordinates: (r * cos(θ), r * sin(θ)).
Pros: Efficient and doesn't require rejection. Cons: Requires slightly more computation than the maximum method.
How it works:
- Generate two uniform random numbers u1 and u2 in [0, 1].
- Set r = u1 + u2.
- If r > 1, reflect it by setting r = 2 - r.
- Generate a random angle θ in [0, 2π].
- Convert to Cartesian coordinates: (r * cos(θ), r * sin(θ)).
Pros: Efficient and doesn't require rejection. Cons: Slightly more complex than other methods.
For each method, we compare the pre- and post-uniformity distributions:
The Reflecting Sum method also uses the Irwin-Hall distribution:
The Python script in this repository does the following:
- Implements each of the four methods for generating random points in a circle.
- Generates a specified number of points (default 3141) using each method.
- Visualizes the results for each method:
- Distribution of points within the circle
- Histogram of radii
- Pre- and post-uniformity plots
- Irwin-Hall distribution for the Reflecting Sum method
- Saves all visualizations as .jpg files in a 'circle_method_visualizations' directory.
- Displays the visualizations on screen.
- Calculates and displays performance metrics (processing time and point density) for each method.
- Provides a detailed explanation of how each method works.
The code uses libraries such as NumPy for numerical computations, Matplotlib for plotting, and PrettyTable for formatting the output table.
To run the code, ensure you have the required libraries installed and simply execute the Python script. It will generate all visualizations, save them to disk, display them on screen, and print explanations and performance metrics to the console.
This project provides a comprehensive comparison of different methods for generating random points in a circle, useful for various applications in mathematics, physics, and computer graphics.
This repository is for educational purposes only.
Copyright 2024 Eric Yocam
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.