diff --git a/X10D/src/RandomExtensions/RandomExtensions.Numerics.cs b/X10D/src/RandomExtensions/RandomExtensions.Numerics.cs index 61fd8dc..b6c47b6 100644 --- a/X10D/src/RandomExtensions/RandomExtensions.Numerics.cs +++ b/X10D/src/RandomExtensions/RandomExtensions.Numerics.cs @@ -31,6 +31,33 @@ namespace X10D.RandomExtensions return new Vector2(x, y); } + + /// + /// Returns a with magnitude 1 whose components indicate a random point on the unit sphere. + /// + /// The instance + /// + /// A whose returns 1, and whose components indicate a random + /// point on the unit sphere. + /// + public static Vector3 NextUnitVector3(this Random random) + { + if (random is null) + { + throw new ArgumentNullException(nameof(random)); + } + + var seed = random.Next(); + var seededRandom = new Random(seed); + + var angle = seededRandom.NextSingle(0, 360).DegreesToRadians(); + var z = seededRandom.NextSingle(-1, 1); + var mp = MathF.Sqrt(1 - z * z); + var x = mp * MathF.Cos(angle); + var y = mp * MathF.Sin(angle); + + return new Vector3(x, y, z); + } } }