/// <summary>
 /// Get a random string
 /// </summary>
 /// <param name="random"></param>
 /// <param name="length">The length of the string</param>
 /// <param name="chars">Available chars</param>
 /// <returns>A random string</returns>
 public static string NextString(this Random random, int length, IEnumerable <char> chars)
 {
     return(new string(new char[length]
                       .Select(selector => random.NextChar(chars))
                       .ToArray()
                       ));
 }
 /// <summary>
 /// Get a random string
 /// </summary>
 /// <param name="random"></param>
 /// <param name="length">The length of the string</param>
 /// <returns>A random string</returns>
 public static string NextString(this Random random, int length)
 {
     return(new string(new char[length]
                       .Select(selector => random.NextChar())
                       .ToArray()
                       ));
 }
 public void Char_Distribution()
 {
     Distribution(
         10,
         1_000_000,
         () => r.NextChar('a', 'k') - 'a'
         );
 }
Example #4
0
        /// <summary>
        /// Generates a random string of a specified length. The default character set is Arabic numberals and upper-case Latin alphabet letters, as used in English.
        /// </summary>
        /// <param name="length"></param>
        /// <param name="charSet"></param>
        /// <returns></returns>
        public static string NextString(this Random rand, uint length, string charSet = null)
        {
            var str = "";

            for (var i = 0; i < length; ++i)
            {
                str += rand.NextChar(charSet);
            }

            return(str);
        }
Example #5
0
        public static object Next(this System.Random generator, Type desired)
        {
            switch (Type.GetTypeCode(desired))
            {
            case TypeCode.Boolean:
                return(generator.NextBool());

            case TypeCode.Byte:
                return(generator.NextByte());

            case TypeCode.Char:
                return(generator.NextChar());

            case TypeCode.DateTime:
                return(generator.NextDateTime());

            case TypeCode.Decimal:
                return(generator.NextDecimal());

            case TypeCode.Double:
                return(generator.NextDouble());

            case TypeCode.Int16:
                return(generator.NextShort());

            case TypeCode.Int32:
                return(generator.NextInt());

            case TypeCode.Int64:
                return(generator.NextLong());

            case TypeCode.SByte:
                return(generator.NextSByte());

            case TypeCode.Single:
                return(generator.NextFloat());

            case TypeCode.UInt16:
                return(generator.NextUShort());

            case TypeCode.UInt32:
                return(generator.NextUInt());

            case TypeCode.UInt64:
                return(generator.NextULong());

            default:
                throw new ArgumentOutOfRangeException("Cannot provide a random " + desired);
            }
        }