You can download the example solutions to see additional details and to experiment with the programs at https://github.com/PacktPublishing/The-Modern-CSharp-Challenge/tree/master/Chapter04.
The following NextDouble
extension method uses the Random
class's existing NextDouble
method to generate a double value within a range:
public static class RandomExtensions { // A Random objects shared by all extensions. private static Random Rand = new Random(); // Return a double between minValue and maxValue. public static double NextDouble(this Random rand, double minValue, double maxValue) { return minValue + Rand.NextDouble() * (maxValue - minValue); } }
The RandomExtensions
class creates a Random
object at the class level. That object is static
, so it is available to all extension methods defined in this class.