I was trying to implement an extension of Random class.
But this function will have one feature, you can get a number followed step. For example:
RandNumb(double min, double max, double step)
RandNumb(1, 10, 2) = 6
RandNumb(100, 1000, 500) = 1000
RandNumb(0.001, 0.1, 0.01) = 0.15
My first idea was to get a random number from min to max, and compare if this is a valid number (because of the step). If not, again generate another number.
But I’m sure that this is not a good performance. What do you think?
By definition of the problem, it seems that the valid values for “RandNumb (1, 10, 2)” would be: 1, 3, 5, 7, 9.
In that case. The code would look like:
And the result would look like this: