#region License Information (GPL v3) /* ShareX - A program that allows you to take screenshots and share any file type Copyright (c) 2007-2020 ShareX Team 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Optionally you can also view the license at . */ #endregion License Information (GPL v3) using System; namespace ShareX.HelpersLib { public static class RandomFast { private static readonly object randomLock = new object(); private static readonly Random random = new Random(); /// Returns a non-negative random integer. /// A 32-bit signed integer that is greater than or equal to 0 and less than System.Int32.MaxValue. public static int Next() { lock (randomLock) { return random.Next(); } } /// Returns a non-negative random integer that is less than or equal to . /// The inclusive upper bound of the random number returned. /// A 32-bit signed integer that is greater than or equal to 0 and less than or equal to . public static int Next(int maxValue) { lock (randomLock) { return random.Next(maxValue + 1); } } /// Returns a random integer that is within a specified range. /// The inclusive lower bound of the random number returned. /// The inclusive upper bound of the random number returned. /// A 32-bit signed integer that is greater than or equal to and less than or equal to . public static int Next(int minValue, int maxValue) { lock (randomLock) { return random.Next(minValue, maxValue + 1); } } /// Returns a random floating-point number that is greater than or equal to 0.0, and less than 1.0. /// A double-precision floating point number that is greater than or equal to 0.0, and less than 1.0. public static double NextDouble() { lock (randomLock) { return random.NextDouble(); } } /// Fills the elements of a specified array of bytes with random numbers. /// An array of bytes to contain random numbers. public static void NextBytes(byte[] buffer) { lock (randomLock) { random.NextBytes(buffer); } } public static T Pick(params T[] array) { return array[Next(array.Length - 1)]; } } }