using System; using System.Collections.Generic; namespace Game.Extensions { /// /// Provides additional methods for the class. /// public static class RandomExtensions { /// /// Generates an infinite sequence of random numbers. /// /// /// The random number generator. /// /// /// The inclusive lower bound of the range to generate. /// /// /// The exclusive upper bound of the range to generate. /// /// /// An infinite sequence of random integers in the range [min, max). /// /// /// /// We use an exclusive upper bound, even though it's a little /// confusing, for the sake of consistency with Random.Next. /// /// /// Since the sequence is infinite, a typical usage would be to cap /// the results with a function like Enumerable.Take. For example, /// to sum the results of rolling three six sided dice, we could do: /// /// /// random.Integers(1, 7).Take(3).Sum() /// /// public static IEnumerable Integers(this Random random, int min, int max) { while (true) yield return random.Next(min, max); } } }