1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-23 01:28:48 +00:00

(#35) Add Random.NextRotation

This commit is contained in:
Oliver Booth 2021-03-10 10:10:22 +00:00
parent 72e24aa791
commit cb7673c9a4

View File

@ -7,6 +7,32 @@ namespace X10D.RandomExtensions
{
public static partial class RandomExtensions
{
/// <summary>
/// Returns a randomly generated rotation as represented by a <see cref="Quaternion" />.
/// </summary>
/// <param name="random">The <see cref="System.Random" /> instance.</param>
/// <returns>
/// A <see cref="Quaternion" /> constructed from 3 random single-precision floating point numbers representing the
/// yaw, pitch, and roll.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
public static Quaternion NextRotation(this Random random)
{
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
var seed = random.Next();
var seededRandom = new Random(seed);
var x = seededRandom.NextSingle(0, 360);
var y = seededRandom.NextSingle(0, 360);
var z = seededRandom.NextSingle(0, 360);
return Quaternion.CreateFromYawPitchRoll(y, x, z);
}
/// <summary>
/// Returns a <see cref="Vector2" /> with magnitude 1 whose components indicate a random point on the unit circle.
/// </summary>