Solutions
 You can download the example solutions to see additional details and to experiment with the programs at https://github.com/PacktPublishing/Improving-your-C-Sharp-Skills/tree/master/Chapter16
22. Random doubles
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.
Note
If you create a new Random
object without passing its constructor a seed value, the class uses the system's time to...