smrendererv3/SMCode/SM.Base/Utility/Randomize.cs
2021-03-17 17:09:59 +01:00

93 lines
No EOL
2.4 KiB
C#

#region usings
using System;
using System.Collections.Generic;
#endregion
namespace SM.Base.Utility
{
/// <summary>
/// A global helper class for randomization.
/// </summary>
public static class Randomize
{
/// <summary>
/// The randomizer.
/// </summary>
public static Random Randomizer = new Random();
/// <summary>
/// Sets the seed for the randomizer.
/// </summary>
/// <param name="seed">The specified seed.</param>
public static void SetSeed(int seed)
{
Randomizer = new Random(seed);
}
/// <summary>
/// Generates a double and checks if its under the tolerance.
/// </summary>
public static bool GetBool(float tolerance)
{
return Randomizer.NextDouble() < tolerance;
}
/// <summary>
/// Generates a integer.
/// </summary>
public static int GetInt()
{
return Randomizer.Next();
}
/// <summary>
/// Generates a integer with a maximum.
/// </summary>
public static int GetInt(int max)
{
return Randomizer.Next(max);
}
/// <summary>
/// Generates a integer with a minimum and maximum
/// </summary>
public static int GetInt(int min, int max)
{
return Randomizer.Next(min, max);
}
/// <summary>
/// Generates a float between 0 and 1.
/// </summary>
public static float GetFloat()
{
return (float) Randomizer.NextDouble();
}
/// <summary>
/// Generates a float between 0 and the specified maximum.
/// </summary>
public static float GetFloat(float max)
{
return (float) Randomizer.NextDouble() * max;
}
/// <summary>
/// Generates a float between the specified minimum and the specified maximum.
/// </summary>
public static float GetFloat(float min, float max)
{
return (float) Randomizer.NextDouble() * (max - min) + min;
}
/// <summary>
/// Gets a random item from the provided list.
/// </summary>
public static TSource GetRandomItem<TSource>(this IList<TSource> list)
{
return list[GetInt(0, list.Count - 1)];
}
}
}