// Copyright (c) 2010-2014 SharpDX - Alexandre Mutel
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
namespace SharpDX
{
///
/// Random functions on commun types.
///
public static class RandomUtil
{
///
/// Gets random float number within range.
///
/// Current .
/// Minimum.
/// Maximum.
/// Random float number.
public static float NextFloat(this Random random, float min, float max)
{
return MathUtil.Lerp(min, max, (float)random.NextDouble());
}
///
/// Gets random double number within range.
///
/// Current .
/// Minimum.
/// Maximum.
/// Random double number.
public static double NextDouble(this Random random, double min, double max)
{
return MathUtil.Lerp(min, max, random.NextDouble());
}
///
/// Gets random long number.
///
/// Current .
/// Random long number.
public static long NextLong(this Random random)
{
var buffer = new byte[sizeof(long)];
random.NextBytes(buffer);
return BitConverter.ToInt64(buffer, 0);
}
///
/// Gets random long number within range.
///
/// Current .
/// Minimum.
/// Maximum.
/// Random long number.
public static long NextLong(this Random random, long min, long max)
{
byte[] buf = new byte[sizeof(long)];
random.NextBytes(buf);
long longRand = BitConverter.ToInt64(buf, 0);
return (Math.Abs(longRand % (max - min + 1)) + min);
}
///
/// Gets random within range.
///
/// Current .
/// Minimum.
/// Maximum.
/// Random .
public static Vector2 NextVector2(this Random random, Vector2 min, Vector2 max)
{
return new Vector2(random.NextFloat(min.X, max.X), random.NextFloat(min.Y, max.Y));
}
///
/// Gets random within range.
///
/// Current .
/// Minimum.
/// Maximum.
/// Random .
public static Vector3 NextVector3(this Random random, Vector3 min, Vector3 max)
{
return new Vector3(random.NextFloat(min.X, max.X), random.NextFloat(min.Y, max.Y), random.NextFloat(min.Z, max.Z));
}
///
/// Gets random within range.
///
/// Current .
/// Minimum.
/// Maximum.
/// Random .
public static Vector4 NextVector4(this Random random, Vector4 min, Vector4 max)
{
return new Vector4(random.NextFloat(min.X, max.X), random.NextFloat(min.Y, max.Y), random.NextFloat(min.Z, max.Z), random.NextFloat(min.W, max.W));
}
///
/// Gets random opaque .
///
/// Current .
/// Random .
public static Color NextColor(this Random random)
{
return new Color(random.NextFloat(0.0f, 1.0f), random.NextFloat(0.0f, 1.0f), random.NextFloat(0.0f, 1.0f), 1.0f);
}
///
/// Gets random opaque .
///
/// Current .
/// Minimum brightness.
/// Maximum brightness
/// Random .
public static Color NextColor(this Random random, float minBrightness, float maxBrightness)
{
return new Color(random.NextFloat(minBrightness, maxBrightness), random.NextFloat(minBrightness, maxBrightness), random.NextFloat(minBrightness, maxBrightness), 1.0f);
}
///
/// Gets random .
///
/// Current .
/// Minimum brightness.
/// Maximum brightness
/// Alpha value.
/// Random .
public static Color NextColor(this Random random, float minBrightness, float maxBrightness, float alpha)
{
return new Color(random.NextFloat(minBrightness, maxBrightness), random.NextFloat(minBrightness, maxBrightness), random.NextFloat(minBrightness, maxBrightness), alpha);
}
///
/// Gets random .
///
/// Current .
/// Minimum brightness.
/// Maximum brightness
/// Minimum alpha.
/// Maximum alpha.
/// Random .
public static Color NextColor(this Random random, float minBrightness, float maxBrightness, float minAlpha, float maxAlpha)
{
return new Color(random.NextFloat(minBrightness, maxBrightness), random.NextFloat(minBrightness, maxBrightness), random.NextFloat(minBrightness, maxBrightness), random.NextFloat(minAlpha, maxAlpha));
}
///
/// Gets random .
///
/// Current .
/// Minimum.
/// Maximum.
/// Random .
public static Point NextPoint(this Random random, Point min, Point max)
{
return new Point(random.Next(min.X, max.X), random.Next(min.Y, max.Y));
}
///
/// Gets random .
///
/// Current .
/// Minimum.
/// Maximum.
/// Random .
public static TimeSpan NextTime(this Random random, TimeSpan min, TimeSpan max)
{
return TimeSpan.FromTicks(random.NextLong(min.Ticks, max.Ticks));
}
}
}