private static double RandomDouble(Attributes.BaseLimitAttribute baseLimitAttribute = null) { if (baseLimitAttribute == null) { return(_random.NextDouble() * (double)_random.Next(Int32.MinValue, Int32.MaxValue)); } if (baseLimitAttribute is Attributes.Double.LimitAttribute limit) { return(limit.MinimumValue + _random.NextDouble() * (limit.MaximumValue - limit.MinimumValue)); } throw new InvalidAttributeException(typeof(Attributes.Double.LimitAttribute)); }
private static int RandomInt32(Attributes.BaseLimitAttribute baseLimitAttribute = null) { if (baseLimitAttribute == null) { return(_random.Next(Int32.MinValue, Int32.MaxValue)); } if (baseLimitAttribute is Attributes.Int32.LimitAttribute attribute) { return(_random.Next(attribute.MinimumValue, attribute.MaximumValue)); } throw new InvalidAttributeException(typeof(Attributes.Int32.LimitAttribute)); }
private static short RandomInt16(Attributes.BaseLimitAttribute baseLimitAttribute = null) { if (baseLimitAttribute == null) { return((short)_random.Next((int)short.MinValue, short.MaxValue)); } if (baseLimitAttribute is Attributes.Int16.LimitAttribute attribute) { return((short)_random.Next(attribute.MinimumValue, attribute.MaximumValue)); } throw new InvalidAttributeException(typeof(Attributes.Int16.LimitAttribute)); }
internal static dynamic GetRandomValues(this Type source, Attributes.BaseLimitAttribute limitAttribute = null) { IDictionary <Type, Func <dynamic> > _actionDictionary = new Dictionary <Type, Func <dynamic> >() { [typeof(string)] = () => RandomString(), [typeof(sbyte)] = () => RandomSByte(), [typeof(short)] = () => RandomInt16(limitAttribute), [typeof(int)] = () => RandomInt32(limitAttribute), [typeof(long)] = () => RandomInt64(limitAttribute), [typeof(bool)] = () => RandomBoolean(), [typeof(double)] = () => RandomDouble(limitAttribute), [typeof(byte)] = () => RandomByte(), [typeof(char)] = () => RandomChar(), [typeof(ushort)] = () => RandomUInt16(limitAttribute), [typeof(uint)] = () => RandomUInt32(limitAttribute), [typeof(ulong)] = () => RandomUInt64(limitAttribute), [typeof(decimal)] = () => RandomDecimal(), [typeof(float)] = () => RandomFloat(), }; return(_actionDictionary.ContainsKey(source) ? _actionDictionary[source]() : GetDefault(source)); }
private static UInt64 RandomUInt64(Attributes.BaseLimitAttribute baseLimitAttribute = null) { ulong minValue = UInt64.MinValue; ulong maxValue = UInt64.MaxValue; if (baseLimitAttribute is Attributes.UInt64.LimitAttribute attribute) { minValue = attribute.MinimumValue; maxValue = attribute.MaximumValue; } else if (baseLimitAttribute != null) { throw new InvalidAttributeException(typeof(Attributes.UInt64.LimitAttribute)); } var byteArray = new byte[8]; _random.NextBytes(byteArray); var result = BitConverter.ToUInt64(byteArray, 0); result = (ulong)(Math.Abs((decimal)result % (maxValue - minValue)) + minValue); return(result); }
private static long RandomInt64(Attributes.BaseLimitAttribute baseLimitAttribute = null) { long minValue = Int32.MinValue; long maxValue = Int32.MaxValue; if (baseLimitAttribute is Attributes.Int64.LimitAttribute attribute) { minValue = attribute.MinimumValue; maxValue = attribute.MaximumValue; } else if (baseLimitAttribute != null) { throw new InvalidAttributeException(typeof(Attributes.Int64.LimitAttribute)); } long result = _random.Next((Int32)(minValue >> 32), (Int32)(maxValue >> 32)); result = (result << 32); result = result | (long)_random.Next((Int32)minValue >> 32, (Int32)maxValue >> 32); result = (Math.Abs(result % (maxValue - minValue)) + minValue); return(result); }