/* * Copyright (C) 2012-2018 CypherCore * * 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 3 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, see . */ using System; using System.Diagnostics.Contracts; public class RandomHelper { private readonly static Random rand; static RandomHelper() { rand = new Random(); } /// /// Returns a random number between 0.0 and 1.0. /// /// public static double NextDouble() { return rand.NextDouble(); } /// /// Returns a nonnegative random number. /// /// public static uint Rand32() { return (uint)rand.Next(); } /// /// Returns a nonnegative random number less than the specified maximum. /// /// /// public static uint Rand32(dynamic maxValue) { return (uint)rand.Next(maxValue); } /// /// Returns a random number within a specified range. /// /// /// /// public static int IRand(int minValue, int maxValue) { return rand.Next(minValue, maxValue); } public static uint URand(dynamic minValue, dynamic maxValue) { return (uint)rand.Next(Convert.ToInt32(minValue), Convert.ToInt32(maxValue)); } public static float FRand(float min, float max) { Contract.Assert(max >= min); return (float)(rand.NextDouble() * (max - min) + min); } /// /// Returns true if rand.Next less then i /// /// /// public static bool randChance(float i) { return i > rand.Next(0, 100); } public static double randChance() { return rand.NextDouble() * 100.0; } /// /// Fills the elements of a specified array of bytes with random numbers. /// /// public static void NextBytes(byte[] buffer) { rand.NextBytes(buffer); } public static T RAND(params T[] args) { int rand = IRand(0, args.Length - 1); return args[rand]; } }