Random Points within Sphere

Most sphere point generators will keep generating points in a box around the sphere until a point is generated that is inside of the sphere. This means that there's a 50% probability for each point to be outside of the sphere, resulting in unstable runtime performance.

This snippet ensures a fixed number of calls to the random number generator and only produces valid points that are inside of the sphere, thus providing better controlled runtime performance.

/// <summary>Returns a random point within a sphere</summary>
/// <param name="randomNumberGenerator">Random number generator that will be used</param>
/// <param name="radius">Radius of the sphere</param>
/// <returns>A random point with the sphere</returns>
public static Vector3 GenerateRandomPointWithin(
  System.Random randomNumberGenerator, float radius
) {

  // TODO: This is just an approximation. Find the real algorithm.
  float r = (float)randomNumberGenerator.NextDouble();
  r = (float)Math.Sqrt(r * r);
  r = (float)Math.Sqrt(r * r);

  return GenerateRandomPointOnSurface(randomNumberGenerator, r);

}