/// <summary> /// Gets a random <see cref="System.Double"/> number within a specific range. /// </summary> /// <param name="rnd">A <see cref="System.Random"/> instance.</param> /// <param name="minValue">The inclusive lower bound of the random number returned.</param> /// <param name="maxValue">The inclusive upper bound of the random number returned.</param> /// <returns>A random <see cref="System.Double"/> number.</returns> /// <exception cref="System.ArgumentOutOfRangeException">Occurs when <paramref name="min"/> is greater than <paramref name="max"/>.</exception> public static double NextDouble(this Random rnd, double minValue, double maxValue) { ExceptionHelper.MinMaxArgumentCheck(minValue, maxValue); if (minValue == maxValue) { return(minValue); } else { return(rnd.NextDouble() * (maxValue - minValue) + minValue); } }
/// <summary> /// Gets a random 64-bit integer within a specific range. /// </summary> /// <param name="rnd">A <see cref="System.Random" /> instance.</param> /// <param name="minValue">The inclusive lower bound of the random number returned.</param> /// <param name="maxValue">The exclusive upper bound of the random number returned. /// <paramref name="maxValue" /> must be greater than or equal to <paramref name="minValue" />. /// <para>Whether this bound is inclusive is determined by the third argument <paramref name="maxValueInclusive" />.</para></param> /// <returns> /// A random 64-bit integer. /// </returns> /// <exception cref="System.ArgumentOutOfRangeException">Occurs when <paramref name="minValue" /> is greater than <paramref name="maxValue" />.</exception> public unsafe static long Next(this Random rnd, long minValue, long maxValue) { ExceptionHelper.MinMaxArgumentCheck(minValue, maxValue); if (minValue == maxValue) { return(minValue); } else { var buffer = new byte[8]; rnd.NextBytes(buffer); fixed(byte *bufferPtr = buffer) { var randomValue = *((ulong *)bufferPtr); return((long)(randomValue % (ulong)(maxValue - minValue)) + minValue); } } }
/// <summary> /// Gets a random integer within a specific range. /// </summary> /// <param name="rnd">A <see cref="System.Random" /> instance.</param> /// <param name="minValue">The inclusive lower bound of the random number returned.</param> /// <param name="maxValue">The upper bound of the random number returned. /// <paramref name="maxValue" /> must be greater than or equal to <paramref name="minValue" />. /// <para>Whether this bound is inclusive is determined by the third argument <paramref name="maxValueInclusive" />.</para></param> /// <param name="maxValueInclusive">Specifies whether <paramref name="maxValue" /> can be returned.</param> /// <returns> /// A random integer. /// </returns> /// <exception cref="System.ArgumentOutOfRangeException">Occurs when <paramref name="minValue"/> is greater than <paramref name="maxValue"/>.</exception> public unsafe static int Next(this Random rnd, int minValue, int maxValue, bool maxValueInclusive) { ExceptionHelper.MinMaxArgumentCheck(minValue, maxValue); if (minValue == maxValue) { return(minValue); } else { var buffer = new byte[4]; rnd.NextBytes(buffer); fixed(byte *bufferPtr = buffer) { var randomValue = *((uint *)bufferPtr); return((int)(randomValue % (uint)(maxValue - minValue + (maxValueInclusive ? 1 : 0))) + minValue); } } }