Random() public static method

Generates a string with a specifies length. Characters will all be printable [A-Za-z]
public static Random ( int length ) : string
length int
return string
Ejemplo n.º 1
0
        private static string RandomString(double min, double max)
        {
            var length = RandomShort(min, max);

            if (max == double.MaxValue)
            {
                length = 10;
            }
            return(String.Random(length));
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Tries to guess at a random data value for a given type.
 /// </summary>
 /// <param name="t">The type to generate data for.</param>
 /// <param name="min">Minimum value.</param>
 /// <param name="max">Maximum value.</param>
 /// <returns>Random data of the requested type.</returns>
 public static object Random(Type t, double min = double.MinValue, double max = double.MaxValue)
 {
     if (t == typeof(bool))
     {
         return(RandomBool());
     }
     if (t == typeof(bool?))
     {
         return(RandomBoolNullable());
     }
     if (t == typeof(byte) || t == typeof(byte?))
     {
         return(RandomByte(min, max));
     }
     if (t == typeof(short) || t == typeof(short?))
     {
         return(RandomShort(min, max));
     }
     if (t == typeof(int) || t == typeof(int?))
     {
         return(RandomInt(min, max));
     }
     if (t == typeof(long) || t == typeof(long?))
     {
         return(RandomLong(min, max));
     }
     if (t == typeof(float) || t == typeof(float?))
     {
         return(RandomFloat(min, max));
     }
     if (t == typeof(double) || t == typeof(double?))
     {
         return(RandomDouble(min, max));
     }
     if (t == typeof(decimal) || t == typeof(decimal?))
     {
         return(RandomDecimal(min, max));
     }
     if (t == typeof(char) || t == typeof(char?))
     {
         return(String.Random(1)[0]);
     }
     if (t == typeof(Guid) || t == typeof(Guid?))
     {
         return(Guid.NewGuid()); // By defination Guids are to be unique
     }
     if (t == typeof(string))
     {
         return(RandomString(min, max));
     }
     if (t == typeof(DateTime) || t == typeof(DateTime?))
     {
         return(new DateTime(Number.Next(1900, 2100), Number.Next(1, 12), Number.Next(1, 29), Number.Next(1, 24), Number.Next(0, 59), Number.Next(0, 59)));
     }
     if (t == typeof(TimeSpan) || t == typeof(TimeSpan?))
     {
         return(new TimeSpan(Number.Next(0, 10), Number.Next(1, 24), Number.Next(0, 59), Number.Next(0, 59)));
     }
     if (t.IsEnum)
     {
         var vals = Enum.GetValues(t);
         return(vals.GetValue(Number.Next(0, vals.Length - 1)));
     }
     if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable <>))
     {
         var t2 = Nullable.GetUnderlyingType(t);
         if (t2.IsEnum)
         {
             var vals = Enum.GetValues(t2);
             var n    = Number.Next(0, vals.Length);
             return(n == 0 ? null : vals.GetValue(n - 1));
         }
     }
     if (t.IsValueType)
     {
         return(0);
     }
     return(null);
 }