1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-10 04:55:42 +00:00

Move RandomExtensions to child namespaces (#7)

This commit is contained in:
Oliver Booth 2022-04-21 17:40:50 +01:00
parent 44c8b87069
commit 95d61eff2c
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
3 changed files with 34 additions and 26 deletions

View File

@ -0,0 +1,32 @@
using System.Drawing;
namespace X10D.Drawing;
/// <summary>
/// Extension methods for <see cref="Random" />.
/// </summary>
public static class RandomExtensions
{
/// <summary>
/// Returns a random color.
/// </summary>
/// <param name="random">The <see cref="System.Random" /> instance.</param>
/// <returns>A <see cref="Color" /> whose red, green, and blue components are all random, and whose alpha is 255</returns>
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
public static Color NextColor(this Random random)
{
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
int seed = random.Next();
var seededRandom = new Random(seed);
var r = (byte)(seededRandom.Next() % (byte.MaxValue + 1));
var g = (byte)(seededRandom.Next() % (byte.MaxValue + 1));
var b = (byte)(seededRandom.Next() % (byte.MaxValue + 1));
return Color.FromArgb(r, g, b);
}
}

View File

@ -1,6 +1,6 @@
using System.Numerics; using System.Numerics;
namespace X10D.SystemNumerics; namespace X10D.Numerics;
/// <summary> /// <summary>
/// Extension methods for <see cref="System.Random" />. /// Extension methods for <see cref="System.Random" />.

View File

@ -1,5 +1,4 @@
using System.Drawing; using System.Globalization;
using System.Globalization;
using System.Text; using System.Text;
namespace X10D; namespace X10D;
@ -52,29 +51,6 @@ public static class RandomExtensions
return random.NextDouble() >= 0.5; return random.NextDouble() >= 0.5;
} }
/// <summary>
/// Returns a random color.
/// </summary>
/// <param name="random">The <see cref="System.Random" /> instance.</param>
/// <returns>A <see cref="Color" /> whose red, green, and blue components are all random, and whose alpha is 255</returns>
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
public static Color NextColor(this Random random)
{
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
int seed = random.Next();
var seededRandom = new Random(seed);
var r = (byte)(seededRandom.Next() % (byte.MaxValue + 1));
var g = (byte)(seededRandom.Next() % (byte.MaxValue + 1));
var b = (byte)(seededRandom.Next() % (byte.MaxValue + 1));
return Color.FromArgb(r, g, b);
}
/// <summary> /// <summary>
/// Returns a non-negative random double-precision floating point number that is less than the specified maximum. /// Returns a non-negative random double-precision floating point number that is less than the specified maximum.
/// </summary> /// </summary>