/* * Util.Random.cs * Authors: Adam Nabinger * Copyright (c) 2007-2008 Cornell University This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ using Microsoft.Xna.Framework; namespace Util { /// /// Static wrapper for randomness /// public static class Random { #region Fields // The random number generator private static readonly System.Random random = new System.Random(); #endregion #region Next /// /// Returns a nonnegative random number (0 to 2,147,483,647). /// /// A 32-bit signed integer greater than or equal to zero and less than MaxValue. public static int Next() { return random.Next(); } /// /// Returns a nonnegative random number less than the specified maximum. /// /// The exclusive upper bound of the random number to be generated. maxValue must be greater than or equal to zero. /// A 32-bit signed integer greater than or equal to zero, and less than maxValue; that is, the range of return values ordinarily includes zero but not maxValue. However, if maxValue equals zero, maxValue is returned. public static int Next(int max) { return random.Next(max); } /// /// Returns a random number within a specified range. /// /// The inclusive lower bound of the random number returned. /// The exclusive upper bound of the random number returned. maxValue must be greater than or equal to minValue. /// A 32-bit signed integer greater than or equal to minValue and less than maxValue; that is, the range of return values includes minValue but not maxValue. If minValue equals maxValue, minValue is returned. public static int Next(int min, int max) { return random.Next(min, max); } #endregion #region NextDouble /// /// Returns a random number between 0.0 and 1.0. /// /// A double-precision floating point number greater than or equal to 0.0, and less than 1.0. public static double NextDouble() { return random.NextDouble(); } /// /// Returns a random number between 0.0 and max. /// /// The exclusive upper bound of the random number to be generated. maxValue must be greater than or equal to zero. /// A double-precision floating point number greater than or equal to zero, and less than maxValue; that is, the range of return values ordinarily includes zero but not maxValue. However, if maxValue equals zero, maxValue is returned. public static double NextDouble(double max) { return max * random.NextDouble(); } /// /// Returns a random number between min and max. /// /// The inclusive lower bound of the random number returned. /// The exclusive upper bound of the random number returned. maxValue must be greater than or equal to minValue. /// A double-precision floating point number greater than or equal to minValue and less than maxValue; that is, the range of return values includes minValue but not maxValue. If minValue equals maxValue, minValue is returned. public static double NextDouble(double min, double max) { return min + ((max - min) * random.NextDouble()); } #endregion #region NextFloat /// /// Returns a random number between 0.0 and 1.0. /// /// A single-precision floating point number greater than or equal to 0.0, and less than 1.0. public static float NextFloat() { return (float)random.NextDouble(); } /// /// Returns a random number between 0.0 and max. /// /// The exclusive upper bound of the random number to be generated. maxValue must be greater than or equal to zero. /// A single-precision floating point number greater than or equal to zero, and less than maxValue; that is, the range of return values ordinarily includes zero but not maxValue. However, if maxValue equals zero, maxValue is returned. public static float NextFloat(float max) { return max * ((float)random.NextDouble()); } /// /// Returns a random number between min and max. /// /// The inclusive lower bound of the random number returned. /// The exclusive upper bound of the random number returned. maxValue must be greater than or equal to minValue. /// A single-precision floating point number greater than or equal to minValue and less than maxValue; that is, the range of return values includes minValue but not maxValue. If minValue equals maxValue, minValue is returned. public static float NextFloat(float min, float max) { return min + ((max - min) * ((float)random.NextDouble())); } #endregion #region RandomVector3 /// /// Returns 3 independant, random floats as a vector3. /// /// The inclusive lower bound of the random number returned. /// The exclusive upper bound of the random number returned. maxValue must be greater than or equal to minValue. /// A vector of 3 single-precision floating point numbers whose values are greater than or equal to minValue and less than maxValue; that is, the range of return values includes minValue but not maxValue. If minValue equals maxValue, minValue is returned. public static Vector3 RandomVector3(Vector3 min, Vector3 max) { return new Vector3(NextFloat(min.X, max.X), NextFloat(min.Y, max.Y), NextFloat(min.Z, max.Z)); } #endregion #region Random Binary /// /// Returns -1 or +1 with roughly equal likelyhood. /// /// -1 or +1 with roughly equal likelyhood. public static int Sign() { unchecked { return (random.Next(2) << 1) - 1; } } /// /// Returns true or false with roughly equal likelyhood. /// /// True or false with roughly equal likelyhood. public static bool Bool() { return random.Next(2) > 0; } #endregion } }