using System;
using System.Collections.Generic;
using System.Linq;
//thanks to chevy ray for this class
namespace Otter {
///
/// Class full of random number generation related functions.
///
public static class Rand {
#region Static Fields
static List randoms = new List();
#endregion
#region Static Properties
static Random random {
get {
if (randoms.Count == 0) {
randoms.Add(new Random());
}
return randoms[randoms.Count - 1];
}
}
///
/// A raw random value.
///
public static float Value {
get { return (float)random.NextDouble(); }
}
///
/// A random float from 0 to 360.
///
public static float Angle {
get { return Float(360); }
}
///
/// Generate a random direction.
///
public static Vector2 Direction {
get { return Util.Normal(Angle); }
}
///
/// Generate a random bool.
///
public static bool Bool {
get { return random.Next(2) > 0; }
}
///
/// Generate a random Color.
///
public static Color Color {
get { return new Color(Float(1), Float(1), Float(1), 1); }
}
///
/// Generate a random Color with a random Alpha.
///
public static Color ColorAlpha {
get { return new Color(Float(1), Float(1), Float(1), Float(1)); }
}
///
/// Generate a random bool.
///
public static bool Flip {
get {
return Bool;
}
}
///
/// Generate a random sign.
///
public static int Sign {
get { return Bool ? 1 : -1; }
}
#endregion
#region Static Methods
///
/// Push a random seed to use for all random number generation.
///
/// The seed.
public static void PushSeed(int seed) {
randoms.Add(new Random(seed));
}
///
/// Pop the top random seed.
///
/// The random seed popped.
public static Random PopSeed() {
var r = random;
randoms.RemoveAt(randoms.Count - 1);
return r;
}
///
/// Generate a random int.
///
/// A random int.
public static int Int() {
return random.Next();
}
///
/// Generate a random int.
///
/// Maximum value.
/// A random int.
public static int Int(int max) {
return random.Next(max);
}
///
/// Generate a random int.
///
/// Minimum value.
/// Maximum value.
/// A random int.
public static int Int(int min, int max) {
return random.Next(min, max);
}
///
/// Generate a random float.
///
/// A random float.
public static float Float() {
return Value;
}
///
/// Generate a random float.
///
/// The maximum value.
/// A random float.
public static float Float(float max) {
return max * Value;
}
///
/// Generate a random float.
///
/// The minimum value.
/// The maximum value.
/// A random float.
public static float Float(float min, float max) {
return min + (max - min) * Value;
}
///
/// Generate a random float.
///
/// A Range that will set the minimum and maximum.
/// A random float.
public static float Float(Range range) {
return range.Min + (range.Max - range.Min) * Value;
}
///
/// Generate a random point inside of a circle.
///
/// The radius of the circle.
/// A random Vector2 position inside the radius.
public static Vector2 CircleXY(float radius) {
return CircleXY(0, radius);
}
///
/// Generate a random point inside a circle.
///
/// The minimum radius.
/// The maximum radius.
/// A random Vector2 position inside the radius.
public static Vector2 CircleXY(float radiusMin, float radiusMax) {
return CircleXY(radiusMin, radiusMax, 0, 360);
}
///
/// Generate a random point inside a circle.
///
/// The minimum radius.
/// The maximum radius.
/// The minimum angle.
/// The maximum angle.
/// A random Vector2 position inside the radius and angle.
public static Vector2 CircleXY(float radiusMin, float radiusMax, float angleMin, float angleMax) {
var angle = Rand.Float(angleMin, angleMax);
var radius = Rand.Float(radiusMin, radiusMax);
return new Vector2(Util.PolarX(angle, radius), Util.PolarY(angle, radius));
}
///
/// Generate a random point in a minimum and maximum set.
///
/// The minimum X value.
/// The maximum X value.
/// The minimum Y value.
/// The maximum Y value.
/// A random position inside the set.
public static Vector2 XY(float xMin, float xMax, float yMin, float yMax) {
return new Vector2(Float(xMin, xMax), Float(yMin, yMax));
}
///
/// Generate a random point in a Rectangle.
///
/// The Rectangle the point will be in.
/// A random position inside the Rectangle.
public static Vector2 XY(Rectangle rect) {
return XY(rect.Left, rect.Right, rect.Top, rect.Bottom);
}
///
/// Generate a random point in a maximum set.
///
/// The maximum X value.
/// The maximum Y value.
/// A random position from 0, 0 to the maximum values.
public static Vector2 XY(float xMax, float yMax) {
return new Vector2(Float(0, xMax), Float(0, yMax));
}
///
/// Generate a random integer point in a minimum and maximum set.
///
/// The minimum X value.
/// The maximum X value.
/// The minimum Y value.
/// The maximum Y value.
/// A random integer position inside the set.
public static Vector2 IntXY(int xMin, int xMax, int yMin, int yMax) {
return new Vector2(Int(xMin, xMax), Int(yMin, yMax));
}
///
/// Generate a random integer point in a minimum and maximum set.
///
/// The maximum X value.
/// The maximum Y value.
/// A random integer position inside the set.
public static Vector2 IntXY(int xMax, int yMax) {
return IntXY(0, xMax, 0, yMax);
}
///
/// Choose an element out of an array of objects.
///
/// The type of object.
/// The array of possible choices.
/// The chosen object.
public static T Choose(params T[] choices) {
return (T)choices[Int(choices.Length)];
}
///
/// Choose an element out of an array of objects.
///
/// The type of object.
/// The array of possible choices.
/// The chosen object.
public static T ChooseElement(IEnumerable choices) {
return choices.ElementAt(Int(choices.Count()));
}
///
/// Choose a random character out of a string.
///
/// The string to choose from.
/// The chosen character as a string.
public static string Choose(string str) {
return str.Substring(Int(str.Length), 1);
}
///
/// Choose a random element in a collection of objects, and remove the object from the collection.
///
/// The type of object.
/// The collection of possible choices.
/// The chosen element.
public static T ChooseRemove(ICollection choices) {
var choice = ChooseElement(choices);
choices.Remove(choice);
return choice;
}
///
/// Shuffle an array of objects.
///
/// The type of object.
/// The array to shuffle.
public static void Shuffle(T[] list) {
int i = list.Length;
int j;
T item;
while (--i > 0) {
item = list[i];
list[i] = list[j = Int(i + 1)];
list[j] = item;
}
}
///
/// Shuffle a list of objects.
///
/// The type of object.
/// The list to shuffle.
public static void Shuffle(List list) {
int i = list.Count;
int j;
T item;
while (--i > 0) {
item = list[i];
list[i] = list[j = Int(i + 1)];
list[j] = item;
}
}
///
/// A random percent chance from 0 to 100.
///
/// Percent from 0 to 100.
/// True if it succeeded.
public static bool Chance(float percent) {
return Value < percent * 0.01f;
}
///
/// Generate a random string.
///
/// The length of the string to return.
/// The set of characters to pull from.
/// A string of randomly chosen characters.
public static string String(int length, string charSet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") {
var str = "";
for (var i = 0; i < length; i++) {
str += charSet[Int(charSet.Length)].ToString();
}
return str;
}
#endregion
}
}