This is the code which I’m using a wrapper extension for a random class.
public static class RandomHelper
{
private static int currentSeed;
private static Random rd = new Random();
public static double Next()
{
return rd.NextDouble();
}
public static double Next(double min, double max)
{
return (rd.NextDouble() * (max - min)) + min;
}
public static double NextOnStep(double min, double max, double step)
{
int range = (int)Math.Floor((max - min) / step);
int stepCount = rd.Next(0, range);
return min + (step * stepCount);
}
public static double NextOnDecimalCount(double min, double max, int decimals)
{
double step = Math.Pow(10, decimals);
return Math.Truncate(((rd.NextDouble() * (max - min)) + min) * step) / step;
}
And imagine this situation, I have a class where contains three ranges of numbers
public class ArithmeticProblemGenerator()
{
Range Number1Range {get;set;}
Range Number2Range {get;set;}
...
Range Number5Range {get;set;}
}
public class Range
{
public Range()
{
}
public Range(double min, double max)
{
this.Min = min;
this.Max = max;
}
public double Min { get; set; }
public double Max { get; set; }
}
And when I want to generate the problem, I add another method in my RandomHelper as extesion.
#region RandomHelper extensions
public static double Next(Range range)
{
return Next(range.Min, range.Max);
}
public static double NextOnStep(Range range, double step)
{
return NextOnStep(range.Min, range.Max, step);
}
public static double NextOnDecimalCount(Range range, int decimals)
{
return NextOnDecimalCount(range.Min, range.Max, decimals);
}
#endregion
But then I add a new feature of my ArithmeticProblemGenerator, I want to have numbers with different decimal places or sometimes the number follow a step.
So, I supposed, will it be good to create another two classes to add the following features.
public class RangeOnStep : Range
{
public RangeOnStep()
{
}
public RangeOnStep(double min, double max, double step)
: base(min, max)
{
this.Step = step;
}
public double Step { get; set; }
}
public class RangeOnDecimalPlace : Range
{
public RangeOnDecimalPlace()
{
}
public RangeOnDecimalPlace(double min, double max, double decimalPlaces)
: base(min, max)
{
this.DecimalPlaces = decimalPlaces;
}
public double DecimalPlaces { get; set; }
}
And add another method extensions with these new classes. Do you think I’m doing a good work or is a mess the design?
I wanna hear suggestions or opinions. Thanks in advance.
method extension isn’t usefull here, why not use a base class range and define overriden method like:
public static class RandomHelper
{
private static int currentSeed;
private static Random rd = new Random();