// Copyright 2018 RED Software, LLC. All Rights Reserved. using System; using System.Security.Cryptography; namespace IgniteEngine { /// /// Class that contains commonly used math functions. /// public class Mathd { /// /// Degrees-to-radians conversion constant. /// public static double Deg2Rad = ((float) Math.PI * 2) / 360; /// /// Radians-to-degrees conversion constant. /// public static double Rad2Deg = 360 / ((float) Math.PI * 2); /// /// Represents the value of 2π /// public static double TwoPi = (float) Math.PI * 2; /// /// A class instance the produces random data. /// private static readonly RandomNumberGenerator RNG = RandomNumberGenerator.Create(); /// /// Returns a random number within the specified range. /// ///The inclusive lower bound of the random number returned. ///The inclusive upper bound of the random number returned. maxValue must be greater than or equal to minValue. public static int Random(int minValue, int maxValue) { return (int) Math.Round(RandomDouble() * (maxValue - minValue)) + minValue; } /// /// Returns a nonnegative random number. /// public static int Random() { return Random(0, int.MaxValue); } /// /// Returns a nonnegative random number less than the specified maximum /// ///The inclusive upper bound of the random number returned. maxValue must be greater than or equal 0 public static int Random(int maxValue) { return Random(0, maxValue); } /// /// Returns a random boolean based on the probability. /// /// The probability of the boolean being true. public static bool RandomBool(double probability = 50) { return Random(100) <= probability; } /// /// Returns a random number between 0.0 and 1.0. /// public static double RandomDouble() { var buffer = new byte[4]; RNG.GetBytes(buffer); return (double) BitConverter.ToUInt32(buffer, 0) / uint.MaxValue; } } }