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.

If you create a new Random object without passing its constructor a seed value, the class uses the system's ...

Get Improving your C# Skills now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.