/// <summary> /// Returns a random string. /// </summary> /// <param name="length">The length of the string.</param> /// <param name="pool">Characters pool</param> /// <param name="alpha">Set to True to use only an alphanumeric character.</param> /// <param name="symbols">Set to true to return only symbols.</param> /// <param name="casing">Default casing.</param> /// <exception cref="ArgumentException">Thrown when <c>length</c> is less than 0.</exception> /// <returns>Returns a random string.</returns> public string NextString(int length = 10, string pool = null, bool alpha = false, bool symbols = false, CasingRules casing = CasingRules.MixedCase) { if (length < 0) { throw new ArgumentException("Length cannot be less than zero.", nameof(length)); } return(string.Join("", NextList <char>(new Func <string, bool, bool, CasingRules, char>(NextChar), length, pool, alpha, symbols, casing))); }
/// <summary> /// Returns a random character. /// </summary> /// <param name="pool">Characters pool</param> /// <param name="alpha">Set to True to use only an alphanumeric character.</param> /// <param name="symbols">Set to true to return only symbols.</param> /// <param name="casing">Default casing.</param> /// <returns>Returns a random character.</returns> public char NextChar( string pool = null, bool alpha = false, bool symbols = false, CasingRules casing = CasingRules.MixedCase) { const string SpecialCharacters = "!@#$%^&*()[]"; string letters, p; if (alpha && symbols) { throw new ArgumentException("Cannot specify both alpha and symbols."); } if (casing == CasingRules.LowerCase) { letters = CharsLower; } else if (casing == CasingRules.UpperCase) { letters = CharsUpper; } else { letters = CharsLower + CharsUpper; } if (!string.IsNullOrEmpty(pool)) { p = pool; } else if (alpha) { p = letters; } else if (symbols) { p = SpecialCharacters; } else { p = letters + Numbers + SpecialCharacters; } return(p[this.NextNatural(max: p.Length - 1)]); }