public static String GetRandomString(Char lowChar, Char highChar, Int32 count, LetterCaseMix letterCaseMix) { if (lowChar >= highChar) throw new ArgumentOutOfRangeExceptionFmt(Properties.Resources.Random_LowCharGreaterThanHighChar, lowChar, highChar); if (count < 1) throw new ArgumentOutOfRangeExceptionFmt(Properties.Resources.Random_CountOutOfRange, count); var result = new StringBuilder(count); Char randomChar; for (Int32 idx = 0; idx < count; idx++) { lock (_semaphore) randomChar = (Char) _rand.Next(lowChar, highChar + 1); switch (letterCaseMix) { case LetterCaseMix.AllUpperCase: result.Append(Char.ToUpper(randomChar)); break; case LetterCaseMix.AllLowerCase: result.Append(Char.ToLower(randomChar)); break; case LetterCaseMix.MixUpperCaseAndLowerCase: if (GetCoinFlip()) result.Append(Char.ToUpper(randomChar)); else result.Append(Char.ToLower(randomChar)); break; default: throw new ArgumentOutOfRangeExceptionFmt(Properties.Resources.Random_UnknownLetterCaseMixValue, letterCaseMix); } } return result.ToString(); }
public static String GetRandomString(Char lowChar, Char highChar, Int32 count, LetterCaseMix letterCaseMix) { if (lowChar >= highChar) { throw new ArgumentOutOfRangeExceptionFmt(Properties.Resources.Random_LowCharGreaterThanHighChar, lowChar, highChar); } if (count < 1) { throw new ArgumentOutOfRangeExceptionFmt(Properties.Resources.Random_CountOutOfRange, count); } var result = new StringBuilder(count); Char randomChar; for (Int32 idx = 0; idx < count; idx++) { lock (_semaphore) randomChar = (Char)_rand.Next(lowChar, highChar + 1); switch (letterCaseMix) { case LetterCaseMix.AllUpperCase: result.Append(Char.ToUpper(randomChar)); break; case LetterCaseMix.AllLowerCase: result.Append(Char.ToLower(randomChar)); break; case LetterCaseMix.MixUpperCaseAndLowerCase: if (GetCoinFlip()) { result.Append(Char.ToUpper(randomChar)); } else { result.Append(Char.ToLower(randomChar)); } break; default: throw new ArgumentOutOfRangeExceptionFmt(Properties.Resources.Random_UnknownLetterCaseMixValue, letterCaseMix); } } return(result.ToString()); }