/// <summary> /// Gets a random bunch of text, size determined by number of words. /// No guarantees are made about the content of the words or any punctuation between the words, /// except that there will be at least a space between every word. /// </summary> /// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="minWords"/> is negative.</exception> /// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="maxWords"/> is less than <paramref name="minWords"/>.</exception> public static string GetWords(this HelpersContainerClasses.Random container, int minWords, int maxWords) { if (minWords < 0) { throw new ArgumentOutOfRangeException(nameof(minWords), "minWords cannot be negative"); } if (maxWords < minWords) { throw new ArgumentOutOfRangeException(nameof(maxWords), "maxWords cannot be less than minWords"); } lock (RandomData.RandomLock) { int wordCount = RandomData.Random.Next(minWords, maxWords + 1); var sourceWords = RandomData.Words.Value; var words = new List <string>(); for (int i = 0; i < wordCount; i++) { words.Add(sourceWords[RandomData.Random.Next(sourceWords.Length)]); } return(string.Join(" ", words.ToArray())); } }
public static byte[] GetBytes(this HelpersContainerClasses.Random container, int count) { var buffer = new byte[count]; lock (RandomData.RandomLock) RandomData.Random.NextBytes(buffer); return(buffer); }
public static T GetRandomItem <T>(this HelpersContainerClasses.Random container, IReadOnlyList <T> fromList) { Helpers.Argument.ValidateIsNotNull(fromList, nameof(fromList)); if (fromList.Count == 0) { throw new ArgumentException("Cannot get an item from an empty list."); } var index = Helpers.Random.GetInteger(fromList.Count); return(fromList[index]); }
/// <summary> /// Gets a random bunch of text, size determined by number of words, limited to a max size. /// No guarantees are made about the content of the words or any punctuation between the words, /// except that there will be at least a space between every word. /// </summary> /// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="minWords"/> is negative.</exception> /// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="maxWords"/> is less than <paramref name="minWords"/>.</exception> public static string GetWords(this HelpersContainerClasses.Random container, int minWords, int maxWords, int maxLength) { Helpers.Argument.ValidateRange(maxLength, nameof(maxLength), min: 0); var value = GetWords(container, minWords, maxWords); if (value.Length > maxLength) { return(value.Substring(0, maxLength)); } else { return(value); } }
public static IList <T> GetRandomItems <T>(this HelpersContainerClasses.Random container, IReadOnlyList <T> fromList, int count) { Helpers.Argument.ValidateIsNotNull(fromList, nameof(fromList)); Helpers.Argument.ValidateRange(count, nameof(count), min: 0); if (fromList.Count < count) { throw new ArgumentException("Not enough items in the list."); } var result = new HashSet <T>(); // This kind of assumes that the fromList is unique. // If not, you will be sorry when you enter an infinite loops here! while (result.Count < count) { result.Add(fromList[Helpers.Random.GetInteger(fromList.Count)]); } return(result.ToList()); }
public static TEnum GetEnum <TEnum>(this HelpersContainerClasses.Random container) { var values = Enum.GetValues(typeof(TEnum)).Cast <TEnum>().ToArray(); return(Helpers.Random.GetRandomItem(values)); }
public static bool GetBoolean(this HelpersContainerClasses.Random container) { lock (RandomData.RandomLock) return(RandomData.Random.Next() % 2 == 0); }
public static double GetDouble(this HelpersContainerClasses.Random container) { lock (RandomData.RandomLock) return(RandomData.Random.NextDouble()); }
public static int GetInteger(this HelpersContainerClasses.Random container, int minValue, int maxValue) { lock (RandomData.RandomLock) return(RandomData.Random.Next(minValue, maxValue)); }
public static int GetInteger(this HelpersContainerClasses.Random container) { lock (RandomData.RandomLock) return(RandomData.Random.Next()); }