This works almost like the disc perimeter random point generation, only that we add a random radius so the points are pulled into the disc.
The only problem is that points would be more dense near the disc's center if the random radius had a linear probability along its range of random values. This happens because one degree sets points further apart on the outside of a disc than it does on the inside. So we need to find a function that will yield a higher probability that points are near the disc's perimeter to compensate:
/// <summary>Returns a random point within a disc</summary>
/// <param name="randomNumberGenerator">Random number generator that will be used</param>
/// <param name="radius">Radius of the disc</param>
/// <returns>A random point within the disc</returns>
public static Vector2 GenerateRandomPointWithin(
System.Random randomNumberGenerator, float radius
) {
// Choose a random angle for the point
float phi = (float)randomNumberGenerator.NextDouble() * MathHelper.Pi * 2.0f;
// Get a random radius with compensated probability for the outer regions of the disc
float r = (float)System.Math.Sqrt(randomNumberGenerator.NextDouble()) * radius;
// Calculate the final position of the point in world space
return new Vector2(
r * (float)System.Math.Cos(phi),
r * (float)System.Math.Sin(phi)
);
}
/// <param name="randomNumberGenerator">Random number generator that will be used</param>
/// <param name="radius">Radius of the disc</param>
/// <returns>A random point within the disc</returns>
public static Vector2 GenerateRandomPointWithin(
System.Random randomNumberGenerator, float radius
) {
// Choose a random angle for the point
float phi = (float)randomNumberGenerator.NextDouble() * MathHelper.Pi * 2.0f;
// Get a random radius with compensated probability for the outer regions of the disc
float r = (float)System.Math.Sqrt(randomNumberGenerator.NextDouble()) * radius;
// Calculate the final position of the point in world space
return new Vector2(
r * (float)System.Math.Cos(phi),
r * (float)System.Math.Sin(phi)
);
}