/// <summary> /// Returns a random double array with option /// </summary> public static double[] NextDoubles(int length, double fromValue, double toValue, RandomNumberOption option) { var ret = new double[length]; for (var i = 0; i < length; i++) ret[i] = fromValue.RandomTo(toValue, option); return ret; }
/// <summary> /// Returns a random float array with option /// </summary> public static float[] NextFloats(int length, float fromValue, float toValue, RandomNumberOption option) { var ret = new float[length]; for (var i = 0; i < length; i++) ret[i] = fromValue.RandomTo(toValue, option); return ret; }
/// <summary> /// Returns a random double array with option /// </summary> public static double[] NextDoubles(int length, RandomNumberOption option) { var ret = new double[length]; for (var i = 0; i < length; i++) ret[i] = RandomNumber.NextDouble(option); return ret; }
/// <summary> /// Returns a random decimal array with option /// </summary> public static decimal[] NextDecimals(int length, decimal fromValue, decimal toValue, RandomNumberOption option) { var ret = new decimal[length]; for (var i = 0; i < length; i++) ret[i] = fromValue.RandomTo(toValue, option); return ret; }
/// <summary> /// Returns a random decimal array with option /// </summary> public static decimal[] NextDecimals(int length, RandomNumberOption option) { var ret = new decimal[length]; for (var i = 0; i < length; i++) ret[i] = RandomNumber.NextDecimal(option); return ret; }
/// <summary> /// Returns a random uint array with option /// </summary> public static uint[] NextUInts(int length, uint fromValue, uint toValue, RandomNumberOption option) { var ret = new uint[length]; for (var i = 0; i < length; i++) ret[i] = fromValue.RandomTo(toValue, option); return ret; }
/// <summary> /// Returns a random uint array with option /// </summary> public static uint[] NextUInts(int length, RandomNumberOption option) { var ret = new uint[length]; for (var i = 0; i < length; i++) ret[i] = RandomNumber.NextUInt(option); return ret; }
/// <summary> /// Returns a random short array with option /// </summary> public static short[] NextShorts(int length, short fromValue, short toValue, RandomNumberOption option) { var ret = new short[length]; for (var i = 0; i < length; i++) ret[i] = fromValue.RandomTo(toValue, option); return ret; }
/// <summary> /// Returns a random sbyte array with option /// </summary> public static sbyte[] NextSBytes(int length, sbyte fromValue, sbyte toValue, RandomNumberOption option) { var ret = new sbyte[length]; for (var i = 0; i < length; i++) ret[i] = fromValue.RandomTo(toValue, option); return ret; }
/// <summary> /// Returns a random uint with option /// </summary> public static uint NextUInt(RandomNumberOption option) { uint result; if (option.HasFlag(RandomNumberOption.CryptographySecure)) { var buffer = new byte[SizeOfUInt]; // create new array to ensure thread safe do { CsPrng.Value.GetBytes(buffer); result = BitConverter.ToUInt32(buffer, 0); } while ( (result == uint.MinValue && option.HasFlag(RandomNumberOption.ExcludedMinValue)) || (result == uint.MaxValue && option.HasFlag(RandomNumberOption.ExcludedMaxValue)) ); } else { do { lock (_lock) { result = Prng.Value.genrand_Int32(); } } while ( (result == uint.MinValue && option.HasFlag(RandomNumberOption.ExcludedMinValue)) || (result == uint.MaxValue && option.HasFlag(RandomNumberOption.ExcludedMaxValue)) ); } return result; }
/// <summary> /// Returns a random long array with option /// </summary> public static long[] NextLongs(int length, long fromValue, long toValue, RandomNumberOption option) { var ret = new long[length]; for (var i = 0; i < length; i++) ret[i] = fromValue.RandomTo(toValue, option); return ret; }
/// <summary> /// Returns a random long array with option /// </summary> public static long[] NextLongs(int length, RandomNumberOption option) { var ret = new long[length]; for (var i = 0; i < length; i++) ret[i] = RandomNumber.NextLong(option); return ret; }
/// <summary> /// Returns a random byte array with option /// </summary> public static byte[] NextBytes(int length, RandomNumberOption option) { var ret = new byte[length]; if (option.HasFlag(RandomNumberOption.CryptographySecure)) { RandomNumber.CsPrng.Value.GetBytes(ret); } else { for (var i = 0; i < length; i++) ret[i] = RandomNumber.NextByte(option); } return ret; }
/// <summary> /// Returns a random float with option /// </summary> public static float NextFloat(RandomNumberOption option) { float result; if (option.HasFlag(RandomNumberOption.CryptographySecure)) ReseedCsPrng(); do { lock (_lock) { result = Convert.ToSingle(Prng.Value.genrand_res53()); } } while ( (result < float.Epsilon && option.HasFlag(RandomNumberOption.ExcludedMinValue)) || (1 - result < float.Epsilon && option.HasFlag(RandomNumberOption.ExcludedMaxValue)) ); return result; }
/// <summary> /// Returns a random decimal with option /// </summary> public static decimal NextDecimal(RandomNumberOption option) { decimal result; if (option.HasFlag(RandomNumberOption.CryptographySecure)) ReseedCsPrng(); do { lock (_lock) { result = Convert.ToDecimal(Prng.Value.genrand_res53()); } } while ( (result == 0 && option.HasFlag(RandomNumberOption.ExcludedMinValue)) || (result == 1 && option.HasFlag(RandomNumberOption.ExcludedMaxValue)) ); return result; }
/// <summary> /// Returns a random double with option /// </summary> public static double NextDouble(RandomNumberOption option) { double result; if (option.HasFlag(RandomNumberOption.CryptographySecure)) ReseedCsPrng(); do { lock (_lock) { result = Prng.Value.genrand_res53(); } } while ( (result < double.Epsilon && option.HasFlag(RandomNumberOption.ExcludedMinValue)) || (1 - result < double.Epsilon && option.HasFlag(RandomNumberOption.ExcludedMaxValue)) ); return result; }
/// <summary> /// Returns a random decimal between a range with option /// </summary> public static decimal NextDecimal(decimal minValue, decimal maxValue, RandomNumberOption option) { decimal result; do { result = NextDecimal(option); result = minValue + maxValue*result - minValue*result; } while ( (result == minValue && option.HasFlag(RandomNumberOption.ExcludedMinValue)) || (result == maxValue && option.HasFlag(RandomNumberOption.ExcludedMaxValue)) ); return result; }
/// <summary> /// Returns a random long from value to value with option /// </summary> public static long RandomTo(this long fromValue, long toValue, RandomNumberOption option) { return fromValue < toValue ? RandomNumber.NextLong(fromValue, toValue, option) : RandomNumber.NextLong(toValue, fromValue, option); }
/// <summary> /// Returns a random long between a range with option /// </summary> public static long NextLong(long minValue, long maxValue, RandomNumberOption option) { long result; if (option.HasFlag(RandomNumberOption.CryptographySecure)) { var buffer = new byte[SizeOfLong]; // create new array to ensure thread safe do { CsPrng.Value.GetBytes(buffer); result = BitConverter.ToInt64(buffer, 0); } while ( (result == long.MinValue && option.HasFlag(RandomNumberOption.ExcludedMinValue)) || (result == long.MaxValue && option.HasFlag(RandomNumberOption.ExcludedMaxValue)) ); } else { do { var d = NextDouble(option); result = Convert.ToInt64(minValue + maxValue*d - minValue*d); } while ( (result == minValue && option.HasFlag(RandomNumberOption.ExcludedMinValue)) || (result == maxValue && option.HasFlag(RandomNumberOption.ExcludedMaxValue)) ); } return result; }
/// <summary> /// Returns a random short that greater than or equal to minValue with option /// </summary> public static short NextShort(short minValue, RandomNumberOption option) => NextShort(minValue, short.MaxValue, option);
/// <summary> /// Returns a random ulong that greater than or equal to minValue with option /// </summary> public static ulong NextULong(ulong minValue, RandomNumberOption option) => NextULong(minValue, ulong.MaxValue, option);
/// <summary> /// Returns a random uint from value to value with option /// </summary> public static uint RandomTo(this uint fromValue, uint toValue, RandomNumberOption option) { return fromValue < toValue ? RandomNumber.NextUInt(fromValue, toValue, option) : RandomNumber.NextUInt(toValue, fromValue, option); }
/// <summary> /// Returns a random uint between a range with option /// </summary> public static uint NextUInt(uint minValue, uint maxValue, RandomNumberOption option) { uint result; do { var d = NextDouble(option); result = (uint)(minValue + maxValue*d - minValue*d); } while ( (result == minValue && option.HasFlag(RandomNumberOption.ExcludedMinValue)) || (result == maxValue && option.HasFlag(RandomNumberOption.ExcludedMaxValue)) ); return result; }
/// <summary> /// Returns a random uint that greater than or equal to minValue with option /// </summary> public static uint NextUInt(uint minValue, RandomNumberOption option) => NextUInt(minValue, uint.MaxValue, option);
/// <summary> /// Returns a random double that greater than or equal to minValue with option /// </summary> public static double NextDouble(double minValue, RandomNumberOption option) => NextDouble(minValue, double.MaxValue, option);
/// <summary> /// Returns a random double between a range with option /// </summary> public static double NextDouble(double minValue, double maxValue, RandomNumberOption option) { double result; do { result = NextDouble(option); result = minValue + maxValue*result - minValue*result; } while ( (result - minValue < double.Epsilon && option.HasFlag(RandomNumberOption.ExcludedMinValue)) || (maxValue - result < double.Epsilon && option.HasFlag(RandomNumberOption.ExcludedMaxValue)) ); return result; }
/// <summary> /// Returns a random double from value to value with option /// </summary> public static double RandomTo(this double fromValue, double toValue, RandomNumberOption option) { return fromValue < toValue ? RandomNumber.NextDouble(fromValue, toValue, option) : RandomNumber.NextDouble(toValue, fromValue, option); }
/// <summary> /// Returns a random decimal that greater than or equal to minValue with option /// </summary> public static decimal NextDecimal(decimal minValue, RandomNumberOption option) => NextDecimal(minValue, decimal.MaxValue, option);
/// <summary> /// Returns a random long that greater than or equal to minValue with option /// </summary> public static long NextLong(long minValue, RandomNumberOption option) => NextLong(minValue, long.MaxValue, option);
/// <summary> /// Returns a random decimal from value to value with option /// </summary> public static decimal RandomTo(this decimal fromValue, decimal toValue, RandomNumberOption option) { return fromValue < toValue ? RandomNumber.NextDecimal(fromValue, toValue, option) : RandomNumber.NextDecimal(toValue, fromValue, option); }