using System; namespace IntegrationTests.Infrastructure { // Thanks BlueRaja - Danny Pflughoeft https://stackoverflow.com/a/13095144/5103354 /// /// Extensions for the random number generator /// public static class RandomExtensions { /// /// Returns a random long from min (inclusive) to max (exclusive) /// /// The given random instance /// The inclusive minimum bound /// The exclusive maximum bound. Must be greater than min public static long NextLong(this Random random, long min, long max) { if (max <= min) throw new ArgumentOutOfRangeException("max", "max must be > min!"); //Working with ulong so that modulo works correctly with values > long.MaxValue ulong uRange = (ulong)(max - min); //Prevent a modulo bias; see https://stackoverflow.com/a/10984975/238419 //for more information. //In the worst case, the expected number of calls is 2 (though usually it's //much closer to 1) so this loop doesn't really hurt performance at all. ulong ulongRand; do { byte[] buf = new byte[8]; random.NextBytes(buf); ulongRand = (ulong)BitConverter.ToInt64(buf, 0); } while (ulongRand > ulong.MaxValue - ((ulong.MaxValue % uRange) + 1) % uRange); return (long)(ulongRand % uRange) + min; } /// /// Returns a random long from 0 (inclusive) to max (exclusive) /// /// The given random instance /// The exclusive maximum bound. Must be greater than 0 public static long NextLong(this Random random, long max) { return random.NextLong(0, max); } /// /// Returns a random long over all possible values of long (except long.MaxValue, similar to /// random.Next()) /// /// The given random instance public static long NextLong(this Random random) { return random.NextLong(long.MinValue, long.MaxValue); } } }